Leonardo thought it was Archimedes (287-212 BC) who invented the cogwheel gears. This would make a neat story, but history is rarely neat. In a similar matter we know that Archimedes did not exactly invent the “Archimedes’s screw”, but rather perfected it, which led to its wide use in the Hellenistic period. One can speculate that something similar happened with cogwheel gears as well, especially complex arrangements of them, but this is only an educated guess. Primitive cogwheels might have been known to Egyptian and Mesopotamian engineers prior to that, just like the screw.
The first written mention of the gear that survives is from Mechanics by Heron of Alexandria (10-70 AD), and Heron is known to rely on Archimedes in other parts of his book. Also, the earliest gear known today comes from the famous Antikythera mechanism, an ancient astronomical calculator, which contained over 30 of them. It is dated to about 150-100 BC and its maker is not known, although Hipparchus (190-120 BC), the father of astronomy, and Posidonius (131-50 BC), the Hellenistic polymath from Rhodes, are likely possibilities.
The mechanism is very complex, “technological artifacts approaching its complexity and workmanship did not appear again in Europe until the development of mechanical astronomical clocks in the fourteenth century”. In other words, gears must have been well known and used by the time it was constructed, one can speculate that earlier Archimedes’s planetarium also used gears. There were reports about an older gear dated to 200-150 BC and possibly connected to Archimedes’s Syracuse, but they are disputable, see discussion at Did Archimedes use epicycles in his planetarium?