We don't even need to be all that close to death for this, just suffer sufficient brain damage. I have a family member that had enough mini strokes that, whenever they have something like an urinary infection, they disassociate from reality and start talking to dead people, and they understand reality as if they were children. It's how we detect infections, as the hallucinations come in pretty early. A course of antibiotics, and they are their normal elderly selves, remembering little of the previous week