Hi J.!
My son was about 2-1/2 when he started doing the same thing. It gradually progressed to include hitting and biting himself, sometimes to the point he would draw blood. I took him to the pediatrician, who sent us to a child behavior therapist. She told me that he was a very sensitive little boy, especially when it came to the feelings and thoughts of others, and that he was acting out against himself to spare me "getting hurt." There wasn't anything wrong with him, he was just the opposite of other children who act out by throwing things or hitting or biting others. When he would get into these little "fits," I would sit, holding him on my lap and hugging him, and we would count. Sometimes it would only be to ten, other times much higher, depending on how upset he was over the situation, but it would always calm him down without him hurting himself.
Now he is 8-1/2, and a wonderful little man. He still tends to overreact and get upset easily, but now all I have to say to him is "let's count" and it's over. He's now even to the point that if he gets extremely upset with me, he'll tell me that he just needs to go count somewhere, and sometimes, my little boy comes back and will snuggle on the couch and count with me if he's upset with someone else!
Hope this helps! Good luck!
K.