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 "somatosensory mapping".
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.
Lloyd, D. M., McGlone, F. P., & Yosipovitch, G. (2015). Somatosensory pleasure circuit: from skin to brain and back. Experimental dermatology, 24(5), 321-324. doi: 10.1111/exd.12639
Free Full Text: https://onlinelibrary.wiley.com/doi/full/10.1111/exd.12639
Free PDF: https://onlinelibrary.wiley.com/doi/epdf/10.1111/exd.12639