It is hard to give a general answer. It wasn't invented, as such, but in the Indo-European linguistic family, a distinction was drawn in the mists of time between an 'animate' and an 'inanimate' class (which is perhaps the most important category distinction that there was to be drawn), and it is from that binary division that a three-fold gender system evolved.
The Afroasiatic languages (including the Semitic languages) already had a grammatical distinction between masculine and feminine nouns many thousands of years before the animate gender has split into masculine and feminine genders in most Indo-European languages.
Because this distinction between masculine and feminine nouns is quite rare among the known language families, it is possible that the appearance of masculine and feminine genders in the Indo-European languages was influenced by the contact with the speakers of Semitic languages (e.g. Akkadian).
It is known that such an ancient linguistic contact has existed, because there are a number of very old reciprocal loanwords between the Indo-European languages and the Semitic languages, dating to about the same time.