What you experienced are known as hypnogogic hallucinations. The precise causes are not well understood, but the phenomenon is not at all uncommon. They are implicated in many so-called "alien abduction" stories.