Well its not from sexual arousal, although a lot of guys use it as an excuse to masturbate. Its kinda like how your nipples get hard when you get REALLY cold, there's usually nothing sexual, just your body doing what it needs to do. But its usually a combination of things. The average guy has eight hard, full-on erections every night. Its just our bodies keeping everything in check. -especially when he's in adolescence with insanely raging hormones-.Aside from that it could be caused by the way an erection puts pressure on the tube going from your bladder, out your body. (urethra) If a guys bladder is full and he's in a deep state of sleep its kinda like a fail safe to make sure he doesn't wet the bed.
We get erections while we sleep so that the penis gets plenty of blood and it keeps everything working. Older guys rarely get erections during the day unless they get some direct sexual stimulation or are watching p*rn so it is necessary for the body to have erections at night. If a guy ignores the erection when he wakes up it goes away in a minute or two.
Hahaha, never heard of morning wood? Yeah, we wake up with boners all the time. It usually just goes away by itself, but if I don't have anywhere to be that day I usually just take care of it right then and there, lol.