Yes I know about serotonin etc, but why does the stimulus of petting a fluffy critter evoke that response in the first place?
My personal uninformed armchair theory: We’re apes, and apes pick bugs out of each other’s fur to bond as a group. But when our ancestors forsook the trees for the plains, we shed our fur to gained sweat glands in order to become the ultimate persistence hunters. Yet the urge to groom remains. We have no fur and we must pet.


As others have said, it’s the oxytocin. You can measure blood serum levels of it in both the human and the dog (that’s the study I remember, probably other mammals would get the same effect) and see it rise when petting occurs. Oxytocin is the ‘love and be loved’ chemical, and that’s a good feeling that your brain will desire to get more of. Classic operational conditioning (as opposed to the classic classic conditioning).
Now, as to why oxytocin is released when the petting is going on? Probably it’s just ‘leftover’ stimuli chains that created the familial bonds when you are held as a baby. It’s not deleterious, so it wasn’t selected against in evolution, and in many ways it is beneficial, so it’s likely that it was a trait that was selected for.
I have not been able to find this again, but I swear that I once read a study where they had measured oxytocin levels while stimulating a badgers nipples. I just want to see the person who did this. Anyways, it worked as expected, and they had some data on which frequency worked best. Iirc it was one slow stroke every 1.5 seconds. Which is kind of the pace I pet cats to relax.
Relevant documentary on The Science of Touch I watched a few years back. I think they mention force and speed matter in stimulating oxytocin.