Baby brains are not developped like ours, so what happens is that at first, your baby was reacting to the oddness of the situation, he was in shock as it was a new situation for him, and was looking for the mother for safety and reassurance. That is why he showed that scared and about to scream face. Then he actually felt the pain of hitting with his head and started screaming/crying because of it. So, in conclusion, your baby felt something is wrong first and then he felt the actual pain which triggered the screaming and crying. Babies often don't see the relation between the two things so, for them, it is two separate things, the event and the bodily reaction to the event.