The Gemara in Masekhet Shabbat (117b) cites a verse from Parashat Beshalach as the source of the obligation to eat three meals on Shabbat. The verse cited is Moshe’s instruction to Benei Yisrael to eat on Shabbat the extra portion of manna that fell the day before: “Eat it today, for today is Shabbat for the Lord; today you will not find it in the field” (16:25). Moshe told the people that no manna would fall on Shabbat, and thus although generally manna that was left overnight became inedible, on Shabbat they must eat the extra portion that fell the previous day, and which would miraculously remain fresh. The Gemara notes that Moshe says the word “hayom” three times in this sentence, and this verse thus alludes to the obligation to eat three meals on Shabbat.
On the basis of the Gemara’s comment, the Levush (291:1) claims that the requirement to eat three meals on Shabbat constitutes a Torah obligation. Most other halakhic authorities, however, as noted by the Mishna Berura (291:1), understood that the Gemara did not intend to actually derive this obligation from the text, but rather cited this verse as an asmakhta (a subtle allusion in the Torah to a law enacted by Chazal).
There is a debate among the halakhic authorities as to the “deadline” by which the third meal must be eaten. The Shulchan Arukh (O.C. 299:1) rules that it is forbidden to begin a meal after sundown, and thus the third meal (se’uda shelishit) must begin before then. However, the Mishna Berura notes that if one had not eaten the third meal before sundown, he should begin the meal even after sundown, as long as it is still within the period of bein ha-shemashot (“twilight,” the period between sunset and nightfall). In his Sha’ar Ha-tziyun, the Mishna Berura observes that the widespread practice is to permit beginning se’uda shelishit shortly after sunset.
The reason why the Shulchan Arukh forbids eating after sundown is because eating is forbidden once the time for havdala has arrived. It is uncertain whether the period of bein ha-shemashot is considered daytime or nighttime, and thus once the sun has set, it might already be time for havdala, in which case eating is forbidden. Due to this uncertainty, one may not begin a meal after sundown. The Sha’ar Ha-tziyun (299:2) notes, however, that since eating se’uda shelishit constitutes a mitzva, and there are some Rishonim who permit eating after sundown on Shabbat, we may rely on those authorities for the sake of fulfilling the mitzva of eating three meals. Although Halakha does not generally follow this view, when the fulfillment of a mitzva is at stake, we may rely on this opinion and permit beginning se’uda shelishit after sundown, during bein ha-shemashot.
The Sha’ar Ha-tziyun also adds another argument for why one may begin se’uda shelishit after sundown: “de-ati safeik asei de-rabbanan ve-docheh safeik issur de-rabbanan.” If a person had not eaten a third meal before sundown, he is in a position of both “safeik mitzva” – where he might have a halakhic obligation to fulfill – and “safeik issur” – he might violate a halakhic prohibition. As mentioned, it is uncertain whether the period of bein ha-shemashot should be halakhically treated as day or as night. Hence, in the case under discussion, it is uncertain whether one still bears an obligation to eat a third meal, since Shabbat might have ended, or might not end until dark. Likewise, since it is uncertain whether Shabbat has ended, we do not know whether or not the time for havdala has arrived, and thus it may or may not be forbidden to eat during this period. The Sha’ar Ha-tziyun writes that just as generally one may fulfill a mitzvat asei (affirmative command) even if this entails overriding a prohibition, similarly, one may perform a “safeik mitzva” even when this entails a “safeik issur.” Since one may be fulfilling the mitzva of se’uda shelishit by eating after sundown, this is allowed even though it may be violating the prohibition against eating before havdala.