Melatonin synthesis occurs primarily in the pineal gland, where it is produced from serotonin through a series of enzymatic reactions, with N-acetyltransferase being the rate-limiting enzyme. This process is tightly regulated by the circadian rhythm, with production peaking at night to help regulate sleep-wake cycles and various biological functions.