It has nothing to do with grammatical gender. It's called phonosyntactic gemination or doubling; it happens in some cases when a word ends in vowel and the next starts in consonant, but it's only written down when the words are joined. So "sopra tutto" became "soprattutto", "da per tutto" became "dappertutto", and so on; "ne il" became "nel" but "ne lo" and "ne la" became "nello" and "nella" (ne is an archaic form of in).