I believe there's no straightforward answer as to „why different usage contexts correlate to different grammatical gender“, but the etymological origin gives some insights to the gender.
Diēs comes from Proto-Indo-European *d(i)jéus „daytime sky, Sky-god“ and is cognate to Iūp-piter (≈ Diespiter, „dies pater“), so originally it should have been masculine, as it is the name of a father god (= Vedic dyauḥ [pitā] or Greek Ζεῦς [πατήρ]):
The Latin forms Diespiter, Dispiter, U. di, dei together with the word diēs ‘day’ point to the generalization of a stem *dijē-, whereas Iūpiter, Iovis reflect PIt. *djow-. These can be derived from a single PIE paradigm for ‘(god of the) sky, day-light’, which phonetically split in two in PIt. and yielded two new stems with semantic specialization. Syllabic *dij- in the nom.acc.sg. can stem from the oblique cases (gen.sg. *diwos, etc.), in which syllabic *di- occurred. The acc.sg. *dijēm led to the creation of a new nom.sg. *dijēs and a separate paradigm meaning ‘day’ <..>
(Michiel de Vaan, Etymological Dictionary of Latin and the Other Italic Languages, Brill, 2008)
As for feminine diēs, I guess that it might be a paradigm-leveling – fifth declension is regularly feminine, diēs is the only masculine exception, so by analogy it would be inclined to become feminine too. However, I am not sure if this is exactly the factor, I couldn't find a source – please suggest one or disprove this.