Fasting is obligatory in the following situations:
Vowed Fasts: If one vows to fast a day or a month to seek nearness to Allah swt, then he must do so due to making that vow. If he sets a specific month or day for the vowed fast, and then ends up fasting before the specified time, then he is rewarded for fulfilling the vow, and the specific time that was originally set is disregarded.
Expiation Fasts: Due to a committed sin, such as: accidental murder, breaking an oath, breaking one’s fast during Ramadan by having sexual intercourse during the day, or vowing to abandon one’s wife sexually and comparing her status to the forbidden status of mother, sister, or daughter.
Witnessing any part of the day or night of Ramadan, according to Imam Hanafi: It is an obligation to fast Ramadan, either by seeing its crescent moon in the case of clear skies, or by completing 30 days of Sha’ban in the case of cloudiness or dust.