Eating or drinking in a moment of forgetfulness, when one totally forgets that he is fasting, the unanimous opinion of all scholars is that such a mistake does not affect the validity of fasting in anyway. The Prophet, peace be upon him, describes the person who makes such a genuine mistake as one who “has been given food or drink by God.” This makes the food or drink taken a gift from God. When God grants someone a gift, He does not question him about using it. The person concerned should simply rinse his mouth and continue fasting till the end of the day.
Needless to say, this must take place in a moment of total forgetfulness, i.e. through a genuine mistake. If there is any suspicion that at the time of eating or drinking, the person was even very slightly aware that he was fasting, then the case is totally different. It is then a case of knowingly ending the fast before time, which is a very grave sin. In such a case, genuine repentance is required before compensation may be offered.