When your muscles don't have enough oxygen for the exercise they're doing they have to break down carbohydrates into something called lactic acid. Lactic acid also increase the acidity of the muscle cells, so it makes your muscles sore. It may seem counterproductive for your body to produce something that would make your muscles not function as well, but without the lactic acid permanent damage could could happen because your muscles wouldn't be getting the oxygen they need.