My oldest daughter done this for about a year. I always talked to her doctor and he would say that she is trying to express her feelings to us. (she always had ear infections up to the age of 3 so she had a really hard time talking and trying to have people around her understand her) If she felt like you did not understand her she would get upset and start banging her head off of things. At other times this is when we could also tell if she was having another ear infection. She would never show the signs of having one. no fever or runny nose.
Other times she would bang her head off of things if she was sleepy. At this time we would just rock her to sleep.
Then there were the times that she just did not get what she wanted. So it is just their way of acting out. Getting attention.
She was the only one of my girls that ever done this. But time will pass and things will get better. Just observe what is going on around your child when he starts to do this. Is he trying to tell you something, is he just doing it to see what it feels like, is he sleepy, or his he having problems trying to tell you something like he wants a drink or he is hungry? Just some things to think of.
But Yes it is a phase a child goes through. It will get better.