It is called benign masochism or hedonic reversal.
While most scientists still do not quite have a handle on the human preference for spicy foods, the best explanation comes from a mechanism called "hedonic reversal", or "benign masochism". Something happens, in millions of humans each year, which changes a negative evaluation into a positive evaluation, like flipping a light switch.
Rozin writes: "If the oral receptors are sending the same message to the brain in the chilli liker and the chilli hater, then the chilli liker must have come to like the very same sensation that the chilli hater, the infant, and nonhuman animals find aversive. One gets to like the burn." Only humans seem to be able to derive pleasure from the negative sensation itself. Animals have been trained to endure self-harm, but only within the context of positive reinforcement.
Also, hedonic reversal applies to enjoying horror movies, sad movies and roller coasters as well. Further reading: http://journal.sjdm.org/12/12502a/jdm12502a.html
Benign masochism refers to enjoying initially negative experiences that the body (brain) falsely interprets as threatening. This realization that the body has been fooled, and that there is no real danger, leads to pleasure derived from “mind over body.”
This can also be framed as a type of mastery. Hedonic reversals have been demonstrated in a number of domains besides irritant spices, including fear and sadness.
In general, it is masochism but the word has stronger connotations as well.
In psychiatry, the terms sadism and masochism describe a personality type characterized by the individual deriving pleasure and gratification from either inflicting or receiving physical pain and/or humiliation, respectively.