Fasting appears in many of the world's religions. I'm curious about the roots of fasting that have been codified into religious practice. Did the ancients discover that fasting was good for you?
No, they saw that hunger is bad and nobody would want to go hungry voluntarily. Suffering for your faith, mortification of the flesh, self-flagellation, etc.
Certainly not a "Diet secret of the ancients", some beneficial side-effects are mostly accidental. (And to be honest, going hungry wasn't exactly hard throughout most history.)
Good question. I think it's a hack: on the one hand it supposedly frees up energy from digestion to concentrate on prayer or repentance, on the other hand I don't feel the thinking is as deep: it locks one into a survival mode thinking - which may or may not help the spiritual practice. Like driving when tired: only the needed parts of the brain that are required switch on. So concentration improves, but the thinking is not as deep (that is when hunger pangs don't distract!)
Supposedly fasting also promotes longevity by affecting gene expression, and the same effects can be obtained from resveratrol. Additionally, small fasts are meant boost human growth hormone, hence the warrior diet: only eating at night. Fasting before and after exercise boosts it the most.
A doctor I once knew said the reason they put new doctors on such long shifts is so they stop thinking and behave more according to text book.
The fact that it was popular doesn't mean it was good. Bloodletting was an extremely popular practice too, but it's now a consensus that's it's harmful except in very specific situations.