I think that the reason is the same as for dreams (eliminating some thoughts and feelings). Through our whole life we’re encounter many thoughts and feelings we might not be aware of. They might not be important for us to let them through our conscious mind or maybe they are too strong and we’re ignoring them to protect our mental sanity, but still they pile up inside our unconscious and when the space gets too small, they need to get out somehow. Since there are laws and social conventions, we obviously cannot express whatever we think and feel so the only way is through nightmares. We need to feel those bad feelings because they already are inside of us and by letting them free, we’ll be a little more relaxed.
I pointed out in an earlier post the importance of paying attention to our thoughts and feelings and to let them express themselves when they appear or as soon as possible. Not doing this creates some of our dreams and most of our nightmares. It’s the first step our brain takes to eliminate the burden. If this fails or it’s not enough, we might experience hallucinations, mental illnesses or even physical diseases. That’s why we see situations when people getting diagnosed with mental disorders after a number of problems in their life.
It’s a tough life and we need to take care of ourselves because no one else will.