User blog comment:Rainboh/Lately I haven't been sleeping/@comment-25941663-20150327172015

Scientists don't really know why we dream, but two of the most prominent theories are these:

A) The brain 'organizes' its memories when we sleep. That results in a lot of brain activity (I can't really remember in which part). To explain that activity, the brain conjures images. That images are the dreams.

B) Dreams, especially nightmares, are a defence mechanism. The brain essentially trains the body to adapt quickly under stressful situations.

If you combine those two theories, you get that the brain tries to make you more stress-resistant by conjuring twisted/scary versions of your own memories.