Coughing increases your intra abdominal pressure, decreases the pressure in the chest when you breath in.
Sometimes this induces reflux
acid to come up into the esophagus. You may induce a gag reflex like this or from thick mucus.
That types of spasms can be a part of a particular illness called pertussis that you could get checked for.