It's hunting behavior, firstly, and defensive behavior secondly. Cats are both hunters (predators) and hunted (prey).
They might be able to kill a mouse with a quick neck breaking shake but a rabbit would take more effort, for example.
In defense if attacked and unable to flee (first reaction) a cat would strike out with claws, if knocked down could roll over and strike with back claws while protecting his or her tender underside.
In the case of play though, I would say it is instinctive hunting behavior. Honing skills.