'Why' is rarely a useful question in English language questions! The honest answer is almost always 'because that is the way it is'.
Yes, you are right. "we should use "the" before "sun", "earth" and "moon", but not for other planet like Mars, Jupiter, Mercury etc".
There are a few others, like "The milky way", and "The southern cross".
In general, you don't use 'the' before simple names. The planets you listed are named after Roman gods.
'The' is a very tricky word.