Sunscreen turns UV light into heat topically, before it enters your cells.
Melanin, the body’s main pigment, turns UV light into heat inside the cells, but outside the nucleus. (The melanin is gathered into a shield-like collection of “melanosomes” which is “above” the nucleus relative to the outside of the body—like this: https://skincancer909.com/wp-content/uploads/2017/03/4K-sunh...) This has the same effect: the UV light never hits the DNA, and so never causes DNA damage.
Melatonin is known to recruit, or cause the production of, melanin within melanosomes. I’m not sure if this is the dynamic being discussed by the paper, though, or whether melatonin is more directly able to act in a similar capacity to melatonin exogenously. If it can, then the same explanation applies. (Does anyone know if pure melatonin powder looks black under a UV filter? That’d suggest it could act directly like melanin, despite its dissimilar chemical structure.)