In short, the circadian rhythm is the internal clock of the body, which can let the body know when to sleep, wake up, and eat. Melatonin is one of the workers of human circadian rhythm.
When it gets dark, the levels of Melatonin in your body begin to rise, signaling your body that it's time to go to sleep. It also binds to related receptors in the body to help the body relax. For example, melatonin binds to related receptors in the brain, helping to reduce neural activity. Binding to related receptors in the eye can help reduce dopamine levels (dopamine is a hormone that helps maintain a clear state). Of course, in the opposite situation, light suppresses the production of melatonin, letting the body know that it's time to wake up.
In addition, Melatonin also helps regulate body temperature, blood pressure, and hormone levels.