"Singultus, as hiccups are known, are contractions of the diaphragm and the muscles between the floating ribs, known as the intercostals. The sharp intake of air causes the vocal folds to resonate, creating the sound which give singultus their common name.
While often presenting nothing more than a nuisance, one group of scientists mention in a study that hiccups can last in some people for days, even weeks. A terrifying prospect.
When the straw is placed inside a glass of water, a pressure valve on the bottom of the straw blocks the water from moving upward as fast as the strength of the human-generated suction would normally allow. This causes the phrenic nerve to activate all of the diaphragm’s capacity, while swallowing uses something called the vagus nerve.
These two parts of the nervous system are the cause for the contractions in the first place, and so keeping them busy prevents them from doing so."
Source here.