Just before falling completely asleep, the mind is in a stage called hypnagogia, which is a phase where a person is not quite awake and not fully asleep. In some, hallucinations can happen during this phase, where it appears as if there is a movie playing, with random images and actions. The body can also make jerky movements, or twitch. Technically hypnagogia is considered an altered state of consciousness. Typically it lasts for about 10 minutes, but this time of being suspended between wakefulness and sleep can be almost like a psychogenic trip. It seems like a dream, but it's actually not, and the difference lies in how active the brain is.
A dream happens when the body has passed into REM sleep.The brain is very active during this phase, and that is what causes dreams to happen, the electrical connectivity in the brain. During hypnagogia, the images are mostly snippets of random items, some from our memory and some that seem unrelated to anything. Painter Salvator Dali is said to have woken himself up from naps to remember and paint the images he saw before falling asleep. Some experts attribute hypnagogia to a gradual powering down of the brain. Different parts of the brain slow down as we fall asleep, it doesn't just happen as if a switch is flipped. This progression of electrical waves in the brain from alpha, to theta, to delta, could be the reason these fugue states happen.