The issue I suspect you are struggling with is the anthropomorphization of the evolutionary process. Evolution is an optimization process driven by random mechanisms...i.e. there is often not a "reason" for why certain things are the way they are. Evolution is not a conscious "designer" that produces things with a specific goal in mind...it simply "picks" the best random alteration and enhances it. If there is no "best mutation", then there is no selection pressure to drive change overtime.
For example, one can ask, "Why do humans have 5 fingers on each hand, instead of 6 or 4?" There is no answer, because the question itself is nonsensical. Humans have 5 fingers simply because there is no pressure to drive a change to 6 or fingers, and so it boils down to randomness. There is no "reason" humans have 2 arms, as opposed to 4. That particular trait just randomly happened to be the one which became dominant.
So, to return to your question of "Why does the brain release DMT when dying?", the answer is that there IS no answer. 335
There is no selection pressure for or against that trait, it just happens. As to the mechanism itself, I suspect that there is just a global breakdown of function in the brain, leading to a dump of ALL neurotransmitters. When the reuptake pumps fail, the neurons just dump their contents into the extracellular space. The result is pleasant hallucinations, giving the impression that humans are not "meant" to feel pain when dying. Imposing that kind of rationale, however, is simply creating meaning out of chaos. If you watch TV static long enough, your mind will start to see patterns which aren't there, simply because the human mind is "wired" to find patterns in chaos in order to better understand the environment.
To wrap this up, I suppose the answer to your question is that the brain is finding meaning in meaninglessness, simply because that is what the brain does. We look for patterns to make sense out of our environment, which sometimes leads to spurious conclusions. There is no reason that the brain hallucinates when dying, but there IS a reason that this trait appears to be a built-in mechanism.
I hope I was able to shed some light on the issue. Or at the very least, not confuse you further :D 335