Pain relief by vibrotactile touch is a common human experience. Previous neurophysiological investigations of its underlying mechanism in animals focused on spinal circuits, while human studies suggested the involvement of supraspinal pathways. Here, we examine the role of primary somatosensory cortex (S1) in touch-induced mechanical and heat analgesia. We found that, in mice, vibrotactile reafferent signals from self-generated whisking significantly reduce facial nociception, which is abolished by specifically blocking touch transmission from thalamus to the barrel cortex (S1B). Using a signal separation algorithm that can decompose calcium signals into sensory-evoked, whisking, or face-wiping responses, we found that the presence of whisking altered nociceptive signal processing in S1B neurons. Analysis of S1B population dynamics revealed that whisking pushes the transition of the neural state induced by noxious stimuli toward the outcome of non-nocifensive actions. Thus, S1B integrates facial tactile and noxious signals to enable touch-mediated analgesia.