During perception, the brain combines information received from its senses with prior information about the outside world. The mathematical concept of probabilistic inference has previously been suggested as a framework for understanding both perception and cognition. Whether this framework can explain not only behavior but also the underlying neural computations has been an open question. We propose that sensory neurons' activity represents a central quantity of Bayesian computations: posterior beliefs about the outside world. As a result, sensory responses, just like the beliefs themselves, should depend both on sensory inputs and on prior information represented in other parts of the brain. We show that this dependence on internal variables induces variability in sensory responses that -- in the context of a psychophysical task -- is related both to the structure of that task and to the neurons' stimulus tuning. We derive analytical predictions for the correlation between different neurons' responses, and for their correlation with behavior. Furthermore, we show that key neurophysiological observations from much studied perceptual discrimination and detection experiments agree with those predictions. Our work thereby provides a normative explanation for those observations, requiring a reinterpretation of the role of correlated variability for sensory coding. Finally, the fact that sensory responses (which we observe) are a product both of external inputs (which we control) and of internal beliefs, allows us to reverse-engineer information about the subject's internal beliefs by observing sensory neurons' responses alone. Population recordings of sensory neurons in animals performing a task can therefore be used to track changes in the internal beliefs with learning and attention.