i agree with Richard Nilsen's answer.
i want to answer with my words, adding some details.
i think, gender is side-effect of fusion.
i think, indo european languages were agglutinative, and have developed into being fusional, and, so, the fusional morphemes has started to modify root morpheme, and they had to modify different root endings differently. for example, if there are words "abo" and "aba", a morpheme "i" better should not make them both "abi", but, for example, "abe" and "abi". so, people have acquired an extra noun categorisation , which has no meaning, but only for technical purposes, and they decided to use that categorisation also for meaning. then, they decided to use one type of root ending as masculine, another as feminine, or another way (for example by count) but not all objects in world are divided by this categorisation, so, as side-effect, they have become "non-natural genders".