Exactly what Riven's said.
Anger, frustration, stress, deep unhappiness, emotional pain, there's plenty of reasons people do it, and of course it's not a healthy way to deal with things, but it helps at the time.
The endorphins released as pain-killers can be calming, soothing. Physical pain takes priority in the mind over emotional pain, so it takes your mind off of things.
It provided a sort of escape, for me. If I was hurting physically, I wasn't hurting emotionally and I was pretty willing to do almost anything to get rid of how I was feeling.
As Riven quite rightly said, it can be addictive. It's a lot harder to stop than it is to start. It's going to be something that'll always be at the back of your mind as the alternative to dealing with how you're feeling or your problems healthily. The 'easy option'. I relied on it, and stopping is definitely not easy.
It'll always be a thing I have to deal with, and I'll always have the scars to remind me of it.
It's not healthy, in the big scheme of things it only adds to your problems, but at the time it's an escape, it's calming, and it appears to help.
http://www.nhs.uk/Conditions/Self-injury/Pages/Causes.aspx