I read an article a while back, than when something really bad happens to the body, the brain looks back into memories to "see" how to solve the problem - maybe it happened before and it will know what to do (like when you cut yourself the second time, you know exactly what to do).
But because it never encountered something like it, it cannot find a solution.
And apparently this is why people when they die see their life flashing before their eyes.