How are tactile pleasure and pain differentiated in the somatosensory cortex? A recent question here  asked about (mostly) how pain and pleasure are differentiated in pathways involving reward/aversion cues. But there was some confusion as to what the question really wanted to ask as it also mentioned &quot;somatosensory mapping&quot;.
A reasonably different but also good question is how are pleasure and pain decoded before it gets to any feedback/reinforcement pathways, i.e. how are pain and pleasure distinguished in the somatosensory cortex. This discussion probably involves considering the way such stimuli are transmitted/encoded from the peripheral nervous system, (e.g. Lloyd, et al. 2015)

The skin senses serve a discriminative function, allowing us to manipulate objects and detect touch and temperature, and an affective/emotional function, manifested as itch or pain when the skin is damaged. Two different classes of nerve fibre mediate these dissociable aspects of cutaneous somatosensation: (i) myelinated A-beta and A-delta afferents that provide rapid information about the location and physical characteristics of skin contact; and (ii) unmyelinated, slow-conducting C-fibre afferents that are typically associated with coding the emotional properties of pain and itch. However, recent research has identified a third class of C-fibre afferents that code for the pleasurable properties of touch - c-tactile afferents or CTs.

To keep answers to this question reasonably short, let's ask just how is tactile pleasure distinguished from (tactile) pain in the brain.
References
Lloyd, D. M., McGlone, F. P., &amp; Yosipovitch, G. (2015). Somatosensory pleasure circuit: from skin to brain and back. Experimental dermatology, 24(5), 321-324. doi: 10.1111/exd.12639Free Full Text: https://onlinelibrary.wiley.com/doi/full/10.1111/exd.12639Free PDF: https://onlinelibrary.wiley.com/doi/epdf/10.1111/exd.12639
Obviously, pain and touch receptors are represented by entirely different receptors in the skin, i.e. nociceptors and mechanoreceptors, respectively. Each has physically different afferents that carry the signal to the brain. Pleasant touch is mediated by C-tactile afferents to the brain, represented by low-threshold non-myelinated C-fibers (Nees et al., 2019. Pain is mainly mediated via Aδ and C fibers (Beissner et al., 2010). Hence, different afferent pathways to the S1 exist from the periphery to the brain.
Once in the brain, there is no such thing as a single localized 'pain center', as proposed by Descartes in the early days (Ha, 2000). In fact, SI cannot be called the 'pain center', as ablation of this cortical region leads to an altered perception of pain, rather than its disappearance (Backonja, 1996). Pain perception involves many more brain centers, including S2, and those involved in higher cognitive and emotional-motivational processes, such as the orbitofrontal cortex (OFC), the anterior cingulate cortex (ACC), the ventral striatum (VS), and the insular cortex (Nees et al., 2019.
In the brain, pleasant touch seems mediated by a set of brain regions overlapping with that of pain, involving the insula, considered to be the primary somatovisceral cortex and connections to the striatum (Migliorini et al., 2013) and ACC. Interestingly, however, pleasant touch was also shown to activate the nucleus accumbens (Kreuder et al., 2017).
In all, to me it seems the pathways largely overlap centrally, barred the activation of the nucleus accumbens for pleasurable tactile stimuli, which is consistent with it being a pleasure center. I think there must be specific connections that are separated very early in the very periphery through a different set of skin receptors that signal to different neurons in brain regions that largely overlap between the two modalities.
References
- Backonja, Pain Forum (1996); 5(3): 174-80
- Beissner et al., Encyclopedia of the Neurological Sciences 2nd ed., 2014
- Ha, MSc thesis, McGill University, Montreal, 2000
- Kreuder et al., Human Brain Mapping (2017); 38(9): 4525-34
- Migliorini et al., Drug and Alcohol Dependence (2013); 133(2): 661-8
- Nees et al., Neurobiology of Pain (2019); 5: 100025