All perfect praise be to Allaah, the Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His Slave and Messenger.
Fasting in such a state is valid as long as the food did not go beyond the mouth and enter the stomach, i.e. it simply remained in the mouth as mentioned in the question. The fast is not broken unless food reaches the stomach. Food that remains only in the mouth for some time does not break the fast.
Both voluntary and obligatory fasting are the same as to what breaks the fast and what is excusable. There is no difference between the matters that break obligatory fasting and those which break voluntary fasting. However, a difference is that it is not incumbent to complete voluntary fast or make up for it if broken before sunset, according to the most predominant opinion of scholars.
Allaah Knows best.