The perception of pain is a multidimensional sensory and emotional/affective experience arising from distributed brain activity. However, the involved brain regions are not specific for pain. Thus, how the cortex distinguishes nociception from other aversive and salient sensory stimuli remains elusive. Additionally, the resulting consequences of chronic neuropathic pain on sensory processing have not been characterized. Using in vivo miniscope calcium imaging with cellular resolution in freely moving mice, we elucidated the principles of nociceptive and sensory coding in the anterior cingulate cortex, a region essential for pain processing. We found that population activity, not single-cell responses, allowed discriminating noxious from other sensory stimuli, ruling out the existence of nociception-specific neurons. Additionally, single-cell stimulus selectivity was highly dynamic over time, but stimulus representation at the population level remained stable. Peripheral nerve injury-induced chronic neuropathic pain led to dysfunctional encoding of sensory events by exacerbation of responses to innocuous stimuli and impairment of pattern separation and stimulus classification, which were restored by analgesic treatment. These findings provide a novel interpretation for altered cortical sensory processing in chronic neuropathic pain and give insights into the effects of systemic analgesic treatment in the cortex.