There are a lot of theories about what exactly the roll of endogenous DMT is, but as far as I know, there hasn't been a whole lot of progress in figuring out what it is for.
Also, many of the original studies that showed you could find DMT in the spine and brain have since been criticized for their faulty methods. The amount of DMT you have in your system at any time may be a lot smaller then a lot of us think.
I'm not a biochemist, but I think it's a little early to be making conjectures about what exactly it is for.
The subjective similarities between a near-death experience and a DMT experience lead me to believe that there may be something to the notion that endogenous DMT is released in instances of serious stress or trauma.
What exactly the purpose of this would be, from an evolutionary perspective is beyond me. There's no real evolutionary benefit to making your last few minutes spiritually ecstatic.
"There are many paths up the same mountain."