Pain feels different in different social contexts, yet the mechanisms behind social pain modulation remain poorly understood. To elucidate the impact of social context on pain processing, we investigated how group membership, one of the most important social context factors, shapes pain relief behaviourally and neurally in humans undergoing functional neuroimaging. Participants repeatedly received pain relief from a member of their own group (ingroup treatment) or a member of a disliked outgroup (outgroup treatment). We observed a decrease in pain ratings and anterior insula (AI) pain responses after outgroup treatment, but not after ingroup treatment. Moreover, path analyses revealed that the outgroup treatment induced a stronger relief learning in the AI, which in turn altered pain processing, in particular if the participant entered the treatment with a negative impression toward the outgroup individual. The finding of enhanced analgesia after outgroup treatment is relevant for intergroup clinical settings. More generally, we found that group membership affects pain responses through neural learning and we thus elucidate one possible mechanism through which social context impacts pain processing.