Have you ever stubbed your toe and then grabbed or massaged your foot to relieve the pain? Or have you ever seen someone hit their own thumb while they were using a hammer and wondered why they would then shake their hand to make the pain go away?
If anything, these actions are counterintuitive methods of pain relief. If you just damaged a body part, wouldn't putting pressure on it or moving it around cause more damage?
Turns out that a lot our behaviour that appears counterintuitive at first glance makes sense once you finally understand the mechanics at work. In this case, when you hit your hand, foot, or any body part, your pain nerves from that body part send a signal through your spinal cord into your brain. This signal is what makes you think "ouch". There are other nerves that also originate in this same body part that send signals when the body part is being touched or moved. When these nerves are activated they temporarily attenuate or reduce the signal being sent from the pain nerves to the brain, giving you temporary pain relief.
Of course, this phenomenon doesn't work in every scenario... if you ever have your arm sawed off... it will hurt, a lot, no matter how hard you shake it...