Endorphins and a sense of release.
It is a bit more complex but it does have to do with physical pain causing a change of brain chemistry. It also has to do (this from my experience) with having something tangible to focus on when there's something you can't change or stop. I had an abusive boyfriend and I would self harm when he yelled at me and it let me be calm while he freaked out because it gave me something to concentrate on and that I think that's the part that felt good. I think there are some physical reasons (screwy brain chemistry maybe) and some emotional reasons. I did something called DBT therapy that really helped me with it. They have a lot of self-help books on DBT stuff on Amazon pretty cheap. Regular therapy doesn't seem to help as much as the DBT.
There is a medication that usually causes the person who is cutting themselves to stop doing it since cutting oneself can lead to an infection or extreme embarrassment if someone sees the marks, however the medication does not address the root of the problem.
I believe that the Contributor who mentioned therapy is absolutely correct. If you or someone you know is cutting or hurting themselves, then counciling and/or therapy is very important to get to the root of the problem and resolve it. At some point, some therapists may ask the cutter if he or she is willing to try naltrexone. It is an opiate antagonist; in other words it blocks the action of opiates including the three opiate agonists our bodies produce naturally. I took a low dose of the drug for another reason and had zero side effects from it.
It is very important for the person exhibiting this behavior to not feel like it is their fault, even though the fact is that most people don't understand. When I informed my parents that I was very ill, by dad literally told me to "Get over it." He doesn't understand that it is a diease no different than MS, Parkinsons or cancer.
I suffer from Major Depressive Disorder very badly to the point that is it extremely difficult for me to do anything. The worst part is that nothing gives me pleasure - I never have any fun and it sucks, but I'm still trying to get better with medication and counciling, so the person doing the cutting should not give up trying to fix it. I know that you'll get better in time and you'll eventually be cured, but you do have to work at it.
I sincerely wish you or your friend my kindest regards.