When a male does not have sexual intercourse or masturbate, the semen will accumulate in his seminal vesicles. When they get full, the semen needs to be ejaculated. The body usually does this during sleep, although emissions while awake have been reported.
The ejaculation during sleep is known as a “nocturnal emission” or as a “wet dream” or “nightfall”.