What you are referring to is likely what we call a “reverse sneeze”. Here is a video that describes it- is this what your dog is doing?
It is very scary for the first time observer, that is for sure! The sound the dog makes can be so scary that many owners have rushed in a panic to emergency clinics in the middle of the night. The good news is, 99% of the time, this is not a serious problem- it is from an itch or irritation at the back of the throat and the dogs use this maneuver to “scratch the itch”.
Dr. Becky Lundgren said it best:
The most common cause of reverse sneezing is an irritation of the soft palate and throat that results in a spasm. During the spasm, the dog’s neck will extend and the chest will expand as the dog tries harder to inhale. The problem is that the trachea has narrowed and it’s hard to get the normal amount of air into the lungs.
Anything that irritates the throat can cause this spasm and subsequent sneeze. Causes include excitement, eating or drinking, exercise intolerance, pulling on a leash, mites, pollen, foreign bodies caught in the throat, perfumes, viruses, household chemicals, allergies, and post-nasal drip. If an irritant in the house is the cause, taking the dog outside can help simply because the dog will no longer be inhaling the irritant. Brachycephalic dogs (those with flat faces, such as Pugs and Boxers) with elongated soft palates occasionally suck the elongated palate into the throat while inhaling, causing reverse sneezing. Small dogs are particularly prone to it, possibly because they have smaller throats.
Reverse sneezing itself rarely requires treatment. If the sneezing stops, the spasm is over. Oftentimes, you can massage the dog’s throat to stop the spasm; sometimes it’s effective to cover the nostrils, which makes the dog swallow, which clears out whatever the irritation is and stops the sneezing. If the episode doesn't end quickly, you can try depressing the dog’s tongue, which opens up the mouth and aids in moving air through the nasal passages. Treatment of the underlying cause, if known, is useful. If mites are in the laryngeal area, your veterinarian may use drugs such as ivermectin to get rid of the mites. If allergies are the root of the problem, your veterinarian may prescribe antihistamines. Because reverse sneezing is not a severe problem, do not worry about leaving your dog home alone; if it occurs when you're not there, the episode will most likely end on its own.
If reverse sneezing becomes a chronic problem rather than an occasional occurrence, your veterinarian may need to look up the nasal passages (rhinoscopy), and may even need to take a biopsy to determine the cause of the problem. Sometimes, however, no cause can be identified.
Some dogs have these episodes their entire lives; some dogs develop the condition only as they age. In most dogs, however, the spasm is a temporary problem that goes away on its own, leaving the dog with no after-effects.
Cats are less likely to reverse sneeze than dogs are. However, owners should always have the veterinarian examine the cat in case it's feline asthma, and not a reverse sneeze. Feline asthma requires more treatment than reverse sneezing does.
I hope this helps!