Our Gemara on Amud Beis discusses the prohibition of tearing or cutting skin out of grief. The verse states (Devarim 14:1):

בָּנִ֣ים אַתֶּ֔ם לַה׳ אֱלֹהקיכֶ֑ם לֹ֣א תִתְגֹּֽדְד֗וּ וְלֹֽא־תָשִׂ֧ימוּ קׇרְחָ֛ה בֵּ֥ין עֵינֵיכֶ֖ם לָמֵֽת׃

⁦You are children of your God. You shall not gash yourselves or shave the front of your heads because of the dead.

Tosafos on Amud Beis (“Deamar”) asks: If so, how can it be that Rabbi Akiva hit himself until he bled over the death of Rabbi Eliezer (See Sanhedrin 68a)? Tosafos offers two answers: (1) Perhaps only tearing skin by cutting is forbidden, while here he was hitting himself out of grief but not using a cutting action, even though he still bled. (2) He was injuring himself over the loss of Torah but not specifically the death of Rabbi Eliezer. The prohibition is only to cut as part of a mourning ritual.

Based on this, Shulkhan Arukh codifies the law (YD 180:5) that it is only forbidden to cut out of grief over a death, or as idolatrous ritual, but not for another loss. Despite this, many poskim hold that there still is a rabbinic prohibition to cut for any reason (See Shach ibid and Kitzur 169:2).

We see that cutting oneself out of overwhelming pain is a basic human response, and not a new phenomenon. However, in different eras and cultures, different forms of mental illness manifest themselves, based on perceptions, beliefs and ego defenses. While the response does seem to be somewhat instinctive, it may represent something different psychologically today than another time. For example, in ancient times a person might consider himself as possessed when overcome with overwhelming urges or intrusive thoughts. Today a person might experience that as addiction, delusional thoughts or obsessive thoughts depending on its severity and behavioral process. 

In a modern psychological sense, people cut as a way to ground themselves. The physical pain or blood might serve as a distraction from greater psychic pain, or might stop a person from dissociating. Disassociation comes about as an ego defense to prevent a person from experiencing overwhelmingly painful memories or feelings. While this is a mental health situation that requires treatment, it is interesting to note that it is part of an ancient human instinct and pattern of behavior.