Dogs grunt when you pet them usually as an involuntary relaxation response. They may be communicating “keep going” or “stop,” or you could be pushing the air out of them or subconsciously reinforcing the behavior. Some dogs are predisposed to grunting but it may also be a health issue.
Why does my dog make groaning noises when I pet him?
If you are petting your dog and he starts groaning, this shows that he is delighted and wants you to continue. This reaction is similar to what human beings experience during a massage when tension is leaving their muscles. Groaning after activity shows your dog is tired and is ready for some rest.
Why does my dog grunt like a pig when I pet him?
These sounds of grunting or oinking are actually called reverse sneezing. Reverse sneezing is when a dog's throat muscles spasm and soft palate are irritated. The dog will breathe in too much air through his nose and thus begin the worrisome sound of your dog sounding like a pig.

Below are two helpful articles on a similar topic 👇
Why do dogs grumble when you pet them?Can playing a musical instrument improve your communication skills?