Rob Dunn traces it:
The first air-breathing fish and amphibians extracted oxygen using gills when in the water and primitive lungs when on landand to do so, they had to be able to close the glottis, or entryway to the lungs, when underwater. Importantly, the entryway (or glottis) to the lungs could be closed. When underwater, the animals pushed water past their gills while simultaneously pushing the glottis down. We descendants of these animals were left with vestiges of their history, including the hiccup. In hiccupping, we use ancient muscles to quickly close the glottis while sucking in (albeit air, not water). Hiccups no longer serve a function, but they persist without causing us harmaside from frustration and occasional embarrassment. One of the reasons it is so difficult to stop hiccupping is that the entire process is controlled by a part of our brain that evolved long before consciousness, and so try as you might, you cannot think hiccups away.
(Hat tip: Hufford)