We developed a technology (capturing activated neuronal ensembles [CANE]) to label, manipulate, and transsynaptically trace neural circuits that are transiently activated in behavioral contexts with high efficiency and temporal precision. CANE consists of a knockin mouse and engineered viruses designed to specifically infect activated neurons. Using CANE, we selectively labeled neurons that were activated by either fearful or aggressive social encounters in a hypothalamic subnucleus previously known as a locus for aggression, and discovered that social-fear and aggression neurons are intermixed but largely distinct. Optogenetic stimulation of CANE-captured social-fear neurons (SFNs) is sufficient to evoke fear-like behaviors in normal social contexts, whereas silencing SFNs resulted in reduced social avoidance. CANE-based mapping of axonal projections and presynaptic inputs to SFNs further revealed a highly distributed and recurrent neural network. CANE is a broadly applicable technology for dissecting causality and connectivity of spatially intermingled but functionally distinct ensembles.