Neurons in cortical circuits are often coactivated as ensembles, yet it is unclear whether ensembles play a functional role in behavior. Some ensemble neurons have pattern completion properties, triggering the entire ensemble when activated. Using two-photon holographic optogenetics in mouse primary visual cortex, we tested whether recalling ensembles by activating pattern completion neurons alters behavioral performance in a visual task. Disruption of behaviorally relevant ensembles by activation of non-selective neurons decreased performance, whereas activation of only two pattern completion neurons from behaviorally relevant ensembles improved performance, by reliably recalling the whole ensemble. Also, inappropriate behavioral choices were evoked by the mistaken activation of behaviorally relevant ensembles. Finally, in absence of visual stimuli, optogenetic activation of two pattern completion neurons could trigger behaviorally relevant ensembles and correct behavioral responses. Our results demonstrate a causal role of neuronal ensembles in a visually guided behavior and suggest that ensembles implement internal representations of perceptual states.