O you who have believed, decreed upon you is fasting as it was decreed upon those before you that you may become righteous. [Surah Baqarah, 2: 183]
In this verse the Creator states that He had enjoined fasting in some form upon the followers of earlier prophets. And He mentions the purpose behind it: "that you may become righteous." So fasting is a means to attain righteousness, piety and consciousness of Allah (taqwá).
In a hadith qudsi, Prophet Muhammad ﷺ disclosed that Allah, the Mighty and Majestic, has said, "Fasting is for Me, and I [alone] reward for it." [Bukhari]