Hiccups are the result of a spasm in your diaphragm. This muscle is located between your chest and abdomen, and its purpose is to help you take air in when you inhale and push it out when you exhale. Sometimes your vocal cords shut and don't let the air go where it's supposed to; the air gets trapped in your throat, and you hiccup. Even though your baby isn't inhaling or exhaling air while it's in utero, it can still get the hiccups.
Researchers aren't sure why babies get the hiccups while they're still in the womb, but one guess is that it's a way for the baby's diaphragm muscles to get into shape for when the baby actually has to breathe. Typically, expecting mothers begin to notice their unborn babies hiccupping during their second trimester. Even so, the fetus can start to hiccup late in its first trimester before the mother can feel it.