P.M.
It's a fairly common behavior kids use to express frustration. Kids learn they can't act out against other people, perhaps, but when they are too exasperated they have to let it out somehow, and that can be against themselves. They may be trying to communicate where words fail. Kids who get spanked are more prone to do this, associating scolding with spanking.
It can also become a means to get a reaction from an adult over time – we teach them that it surprises, worries, or angers us, and they start doing it as a means to get attention.
My grandson would occasionally do this between the ages of 1.5 and 3.5. We realized it happened when he really wanted something he couldn't have, or when we required him to stop playing to get on our schedule. At any rate, your son may be feeling a mighty sense of injustice. And some kids don't respond well to punishment, time-out, scolding.
A very good way to eliminate this behavior is to give him more choices, give him as much warning as possible when you'll need him to stop what he's doing, empathize with his wants, even if you can't give them to him (many children can calm down pretty well if they only believe you've really listened to what they want). Also, don't react at all when he hits himself – no sympathy OR scolding.
I've been using the techniques in the practical book How to Talk So Kids Will Listen, and Listen So Kids Will Talk. It coaches you along so you can immediately start incorporating the simple and sensible examples into your everyday situations.