Sun darkens skin because it triggers the production of melanin, a brownish-black pigment that helps filter out harmful ultraviolet rays. It lightens hair because the UV light triggers the breakdown of these selfsame melanin molecules into simpler and evidently less colorful compounds.
The exact mechanism by which this is accomplished is not as clear as it might be. “The ionic pathway probably begins by nucleophilic attack of the peroxide anion on the o-quinone grouping,” says one medical text, clearly written by the kind of guy you wouldn’t want to have season tickets next to at the ballgame.
The melanin in both skin and hair is meant to protect the other tissue, but in skin it’s renewed (and thus the skin gets darker) whereas in hair it’s not, since hair is no longer living.