I'd say in this case the gender of the nouns follows the sex of the gods of those heavenly bodies. In most Indo-European derived cultures the sun god is masculine.
It may be that the way divinities attach to objects in pre-Christian societies determines the gender of the nouns. So whether the god of the hearth is male of female determines what the gender of that noun is.
I think this is a very interesting subject.