The answer to your question is, "probably by accident". Then those discovering it tried it and found it worked well with their digestion when it was mixed with other foods. In certain parts of the world where it is warm, milk was transported in leather bags bound to animals that were ridden as transportation.
The gentle agitation and natural bacteria (Lactobacillus bulgaricus and Streptococcus thermophilus common in yogurt) caused the milk to set up or coagulate. Thus yogurt or some kind of cheese.
I've made a lot of yogurt and usually make mine from powdered milk....adding extra powder beyond what is called for to just make milk. This is heated to 185 degrees F and then cooled down to 105 Degrees F before stirring in a large spoon full of live yogurt to inoculate this mixture. It can then be set in a heated oven that is about 100 Degrees F overnight. 8 to 9 hours will yield a less tart yogurt....where 10 to 12 hours will be more sour.
To flavor it you can use sugar or artificial sweetener and flavoring extracts. Lemon, Chocolate, Orange, instant coffee crystals can be used and so on.
I used to take pie filling from the can and mix it with some simple sugar syrup and place it in the bottom of a cup. Then refrigerate it to where it was more
solid. Then the warm yogurt mix was poured over the back of a spoon to gently fill the cup. I used "Old Fashion" glasses from the bar glass ware. After 9 hours in the warm oven on a cookie sheet the yogurt was set up firm and I could refrigerate it. (I covered the top with plastic wrap and held it on with rubber bands.
If you want to try making it from powdered milk start with 3 3/4 C warm water, 1 C powdered milk, 1/3 C of live culture yogurt. (like Dannon plain)
When I cost my yogurt out I figured it was costing me from 27 cent a cup of yogurt to 33 cent depending on what pie filling I was using. Those flavored with extracts were anywhere from 12 cent to 19 cent.
If you shop yard sales & flea markets/Goodwill etc you may find one of these Yogurt Makers that plugs into the wall. A simple appliance with jars and lids. I've found these for as little as $3 bucks in almost new condition. These are often given as gifts...that never get used.