I'll try to explain it from my point of view bearing in mind I'm only one person so this is only one perspective.
When I was young and had no idea I was a person, (before age 10) other people were just things, not people. Yes they moved, they did things, they said things, but why? It was a mystery. I knew that if I did or said the wrong thing, something unpleasant would happen very fast, so I learned all sorts of avoiding-things gradually. Hard when I could only focus on one small thing at a time, though.
I remember getting a caning for picking up a pair of scissors in the classroom and cutting some of the hair off the child next to me. Well, I was totally utterly focused on what the hair felt like and looked like, and I knew that people cut hair. I didn't know that it was wrong to pick up scissors and cut some off. I'd forgotten I was in a class because my focus was totally on the hair and the scissors. I was about 9 at the time. That sort of thing happened a lot.
I know that when I'm in any noisy environment full of people, the stress level on me is more than I can take. It sounds like being at the front row of a rock concert. If someone jostles me slightly, it's like being punched. I know that if there's also strong smells, that doesn't help. Neither does me having to do manual things - I get them wrong. I react by withdrawing into myself, but boys may (not always) react by trying to get the stress away from them. If they hit it, it runs away, so removes its noise and chaos and pain from them a bit.
Or they may hit because the child says or does something interesting as a result. "I hit the child, their face gets wet and they make a squeaky noise. If I do it again, maybe it'll happen again - let's test it."
Either theory explains some of it, according to various things I've read and some of my own experiences and talking with my ds now he's older.