Dreams are the essential process by which we stay sane. Every day, our subconscious mind rearranges all the day's activities to make sense of it all and any really bad stuff is suppressed out of the conscious mind. What SimplyGrand says is correct too - memories are made of this. Dreaming is an extraordinarily complex process which is ESSENTIAL for humans. If we are deprived of dreaming for any length of time, we get very irritable and aggressive and, when allowed to sleep, will dream continuously until the "dream debt" is made up. Recurring dreams are interesting, too. That means you have an unresolved conflict that your mind is ruminating over until it finds a solution.
Hmm... Scientifically. Well psychology is about as close to scientific as you can get with this, even though it is not a true branch of science. There are quite a few theories about it; last thing you thought of before sleeping, wants, desires and needs, entirely random thought processes, reliving memories. Take your pick! No one really knows much about it except that it happens in REM sleep (Rapid Eye Movement), which is the period when you have the most brain activity while still being asleep.