Here's something I read recently, don't know how accurate this might be, but thought I'd paraphrase it as this question comes up a lot!
When a cat is a kitten, its mother will groom it and it feels safe and comforted, it has nothing to worry about, its mum will protect it. As it grows up, its instinct as a territorial predator kicks in, and that instinct tells it that it is not safe to relax completely, always territory must be protected and the next meal sought. Even when asleep, ears are alert to sounds of movement. When a socialised cat lives in a safe loving home, it is kept partially in that kitten state of trust and protection, it enjoys the feeling of being groomed, it kneads with its paws, it feels safe and comforted as if it were still 3 weeks old. The adult instinct creates a conflict with this, the cat is instinctively torn between wanting to relax completely like a kitten and wanting to stay alert like a predatory adult - and when this internal conflict and confusion occurs at the point of ultimate relaxation, the adult cat reacts against it and nips!
As I say I am not sure entirely what to make of that, but it is one theory.