When you scratch a dog on its belly, it will kick or shake its legs due to a scratch reflex. This reflex is a completely involuntary reaction.
When a dog's belly is being scratched, it causes a feeling similar to that of an itching sensation. Instinctively, the dog will shake its leg as if to scratch the itch, just as it would any other itch it can feel on its body. This reflex is beneficial to the dog's health, as it is used to assess spinal cord and nerve function. However, this reflex does not hurt the dog, and a little belly rubbing does a dog a lot of good.