My daughter was a "head banger". It's really disturbing, but, fairly common. Just make SURE you don't reward him for the behavior (like by letting him out of time out, or giving him attention for hitting himself.) You might make sure you put him in time out in a place it's hard to get a good head whack in - like the couch or an easy chair away from the wall.
We finally got our daughter to stop by saying very, very sternly and deeply "NO!! Do not bang your head." It was the sternest, loudest, most serious voice we ever used with her. It did not work overnight, but, while she had some doozies for bruises on her head, she never seriously hurt herself.
Just for the record, though, I'd report it to your pediatrician so that someone outside the family knows about this pattern of behavior. Our pediatrician, in the nicest way, just cracked up and said "Yeah, a lot of kids do that, my third one did..."