There’s no definitive answer to this. The common explanation is that any out-of-the-world or altered perception of reality is due to hallucinations induced by enhanced brain chemistry while encountering a near-death experience.
Explaining the source or rationale behind the hallucination is even more complex as they are influenced by a lot of physiological and psychological factors, including cultural exposure, memories, trauma, stress, preconceived spiritual and social beliefs and expectations about death, all causing a rush of heightened brain activity that creates neural pathways, giving a unique experience for every individual.