It's so fundamental to our society now, but I just don't see why humans would start catching dirt on fire. Apologies for the ignorance.
I answered a similar question in /r/AskAnthropology last week:
Once upon a time people would answer with this question with a shrug and something about leaving shiny rocks in a campfire, but thanks to recent archaeometallurgical research we actually know quite a bit about how it came about. It's my favourite example of how technological innovation—even in so-called "primitive" societies—is embedded in a complex web of cultural and historical contingency, and rarely just a case of people accidentally dropping things in fires (or leaving them out to spoil, etc.)
So although extractive metallurgy (smelting) was invented independently several times, the earliest known and best-studied example is from the Neolithic of Southeastern Europe. It's from there that the idea spread to the rest of Europe and probably also the Near East. But before we get to that let's backtrack a bit. Humans have been been familiar with broad concept that fire can have a desirable, transformative effect on materials for a very, very long time. We were using it to cook food millions of years ago. Later they discovered it worked on inorganic material too: heat-treating flint and chert makes it harder and easier to flake into stone tools. It also changes its colour, and from at least 12,000 years ago people were deliberately taking soft stones, grinding them down, then burning them to produce brightly coloured decorative beads. Finally, around 8,000 years ago, there was a crucial development when people discovered that firing clay produced hard, watertight ceramics. This had actually been discovered several times before, but it really took off at that time because of another recent invention – farming. Pottery made agriculture much easier, allowing people to store food, protect it from pests, and cook it in open fires by boiling or steaming.
At the same time as people were experimenting with fire-treated material, they were becoming familiar with metal ores. Oxides were frequently ground to make decorative dyes, powders and paints; or shaped into beads. By about 11,000 years ago they were also exploiting the small amounts of native gold, silver, copper and (meteoric) iron available on the earth's surface to make beads, jewellery and other personal adornments using "cold" (i.e. low temperature, non-extractive) metallurgy.
But what about extractive metallurgy? Well, when the first farmers of the Near East migrated to Europe, they brought all these technologies with them. And their descendants in Southeastern Europe became very keen on ceramic technology. The produced a wide variety of vessels, but also used it in the construction of their homes, to make floors and furniture, and in domestic rituals centred around miniature ceramic figurines. High quality pottery needs to be fire at controlled, high temperatures, so they developed sophisticated kilns fuelled with charcoal. They were also very keen on decorating their ceramics, and the fashion at the time was for bright, colourful and/or shiny patterns.
So at this point (around 7,000 years ago) all the elements are in place: you have a group of highly skilled ceramicists with sophisticated kilns, who were familiar with a variety of colourful metallic ores and were constantly experimenting with new ways to produce eye-catching pottery. And in the area of modern-day Serbia, this culture happened to coincide with a natural abundance of copper ore deposits. In this context, Radivojević & Rehren (2015) describe how the first step towards extractive metallurgy may have happened:
Although the original reasons for applying heat (as in smelting) to copper minerals are not yet well understood, a clue for such a decision may be found in experimental reconstructions of copper smelting events. The primary author recently took part in a copper smelting experiment and recorded the moment when the flames of this process went green (Fig. 14). This happened just after a handful of malachite was thrown onto a bed of charcoal heated up in a small bowl furnace. Thus, it could be assumed that early attempts to smelt copper were potentially driven by curiosity to explore properties of materials that burned with green flames, and that knowledge of copper smelting evolved in the context of appreciation of the aesthetics of coloured flames.
Our first evidence for it comes in the form of small amounts of slag and tiny copper droplets in ad hoc, but efficient, ceramic-lined pit-kilns. They only used ores with distinctive black and green colours, so perhaps these were the remains of one-off, experimental or preformative episodes like the one Radivojević describes.
Later, as they gained an understanding of the smelting process, they began to use more complex ores and were able to produce a substantial amount of copper implements. But overall they were still rare, and probably inferior to lithic tools in many respects, so presumably they were valued for the exotic and aesthetic properties. Metalworking itself probably remained the closely-guarded and ritualised secret of a small number of specialists – some of the oldest European folk-tales, dating back as far as the Bronze Age, are about smiths using otherworldly knowledge to outsmart devils.
To summarise: as far as we know the earliest use of smelting was in Southeastern Europe around 7,000 years ago. It was invented by skilled craftsman by a process of deliberate experimentation, drawing on a sophisticated knowledge of pyrotechnology, the aesthetic and material properties of metal ores and "cold" metalworking that went back thousands of years. At the same time, it was also the product of a unique and fortuitous combination of existing technical skills, cultural values and natural resource distribution.
Sources:
You may get better answers in /r/AskAnthropology. The earliest smelting pre-dates history, back around 9000 BCE. So we don't have a written record of why or how the practice started. That makes this more an anthropological/archeological kind of question.