Melatonin is derived from the amino acid tryptophan. It is released rhythmically from the pineal gland into the bloodstream. Melatonin levels are highest at night. This pattern which cycles daily is known as a circadian rhythm.
Studies indicate that there are two factors that produce this daily change in melatonin blood levels. It appears there is an endogenous rhythm produced by rhythmic nerve activity in the suprachiasmatic nucleus (SCN), a part of the brain thought to be the seat of the body's internal biological clock. Studies on blind subjects, where there is no visual input to the pineal gland, indicate melatonin levels rise and fall following a 25 hour rhythmical pattern, presumably of an endogenous origin.
In addition, the anatomical link between the retina of the eyes and the pineal gland (as described in the anatomy section) means that the daily light/dark cycle can influence the activity of the pineal. In the dark that pineal gland is more active, in the light it is less active. Therefore melatonin levels are raised in the dark and lowered in the light. In sighted people where this connection is intact and functioning, the melatonin levels follow a 24 hour pattern rather than a 25 hour one. It is as if the visual information on the time of day entrains the endogenous rhythm produced by the SCN to the environmental light/dark cycle, matching up the body's internal clock with the external environmental clock. It is easy to see why this would evolve. Humans have developed to be animals active during the day. Melatonin, as will be discussed, promotes sleep. So, it is best to make the circadian rhythm of melatonin levels such that they are higher at night and lower in the day promoting alertness during daylight activity and promoting recuperative sleep during darkness when there is less danger from predators.