It happens when you wake up after already falling asleep.

This happens during one of the two transitions while in your natural sleep cycle. When you enter into REM your body must eventually come out of it, but sometimes your body has difficulty making the transition.

