Chocolate, as you probably know, comes from cocoa trees, but to get from the tree to edible chocolate takes a fair bit of processing. In short, the cocoa beans are removed from the pod, fermented, dried, roasted and shelled, ground, and sweetened.
I can understand why ancient people paid any attention at all to these seed pods; when you cut through the thick rind, the white pulp the seeds are embedded in is delicious straight off the tree. However, the seeds are pretty bitter and nasty tasting when raw. How did they ever go from "suck on this but don't crack the seed open" to "ferment+dry+roast+grind+sweeten = delicious"?
Fermentation has been around since approximately forever, specifically the end of the stone age and the rise of deliberate farming instead of gathering wild plants, so there's a good chance they tried fermenting the pulp. Sweet things ferment easily into alcohol, and that stuff is popular. The fermentation step in chocolate production actually involves fermenting the pulp so it liquifies and runs off the seeds, which is a very easy way to clean and dry the seeds (cocoa beans): just put them in a pile and wait.
I could see somebody doing that to make a fermented pulp drink, then wondering if there was anything that could be done with those darn seeds that were left over. Of course, they tasted nasty when raw.
When I stop to think about it, I realize that when the cocoa plant was first discovered, humans had been cooking their food for a very long time, even longer than they'd been fermenting it, and had certainly already discovered that some things were inedible raw but tasty when cooked. Now it's not such a stretch to add roasting after fermenting.
Once the inedible shell is removed, the little edible bit inside can be eaten. To modern tastebuds it's still strong and bitter, but then so is coffee and it's pretty popular. You can buy cocoa nibs and use them like many nuts.
The grinding step probably was added by somebody experimenting. Maybe they wanted to mix cocoa nibs in with something else, without the lumpiness, who knows. When cocoa nibs are ground, they make chocolate liquor, a liquid mixture (not an alcohol) of cocoa butter and cocoa solids. You may recognize those three names from the ingredients list of quality chocolate bars.
The Aztecs drank xocoatl, which is nothing at all like hot chocolate, because they didn't have sugar. Instead, they would spice it, including with hot chili peppers, to make their drink. A conquistador brought it back to spain and added sugar, whereupon it was a hit with the nobility.
And there you have it: fermentation, roasting, grinding, and sweetening, each one a fairly reasonable step, when before I started writing this article I knew what the steps were but stopped at "how on earth did they figure that out?"
Now I want to learn how people went from cassava root, which contains poison, to tapioca, which does not. The poison is fairly easy to remove, but what made somebody try to make a poisonous root into a safe food? Some other day, perhaps. At least cocoa pods have a tasty pulp to kick-start interest in the plant.