What you are referring to is called "Nocturnal penile tumescence". It happens to pretty much every man, usually a few times a night.
Wikipedia says "The cause of NPT is not known certainly. Bancroft (2005) hypothesizes that the noradrenergic neurons of the locus ceruleus are inhibitory to penile erection, and that the cessation of their discharge that occurs during REM sleep may allow testosterone-related excitatory actions to manifest as NPT."
Basically, when you are awake, there are certain chemicals that inhibit erections unless you are aroused, but when you sleep those chemicals go away, and it can manifest itself as an erection. [ jediforhire's advice column | Ask jediforhire A Question ]
polopunk answered Monday February 16 2009, 10:05 pm: guys cannot pee and have an erection at the same time, so when guys get older instead of wetting there bed, they get an erection.
its really simple [ polopunk's advice column | Ask polopunk A Question ]
Alin75 answered Sunday February 15 2009, 11:06 pm: Found a very good answer here:
Attention: NOTHING on this site may be reproduced in any fashion whatsoever without explicit consent (in writing) of the owner of said material, unless otherwise stated on the page where the content originated. Search engines are free to index and cache our content. Users who post their account names or personal information in their questions have no expectation of privacy beyond that point for anything they disclose. Questions are otherwise considered anonymous to the general public.