Like Rambam, Ralbag also maintains that this incident did not occur in real life, and he explicitly discusses the view of the Sages:
והנה דעת רבותינו ז”ל הוא שזה הענין היה כפשוטו ולזה אמרו שפי האתון הוא מן הדברים שנבראו בין השמשות
והנראה בעינינו לפי השורשים האמיתיים הנראים מדברי הנבואה ומן העיון שזה הסיפור היה ענין שקרה לבלעם במראה הנבואה
Behold, the view of the Sages of blessed memory is that this thing was like its straightforward meaning. And therefore they said that the mouth of the donkey is of the things created at twilight. But what appears in our eyes based on the true principles that are apparent from the words of the prophecy and from philosophy is that this incident was something that occurred to Bilam in a prophetic vision.
Ralbag here openly acknowledges that he is disagreeing with the Sages, and that doesn’t seem to bother him. This is apparently because the interpretations that deny that events literally occurred as described in Scripture are primarily based on philosophy, an area in which the Sages don’t necessarily have any special authority. Indeed, elsewhere Ralbag quotes the Sages and very strongly disagrees with them based on a fundamental philosophical principle. In that instance he specifically states that in these types of matters we are not bound by the statements of the Sages... and he cites Rambam as the source for this:
אלא שבאלו הענינים ובכיוצא בהם לא נביט למאמר אומר כמו שהורנו הרב המורה אבל נמשך אל מה שיאות על שרשי התורה והעיון
But in these matters and the like we do not look at who says it, as Rambam has taught, but we follow that which fits with the principles of Torah and philosophy.
Thus, the most basic answer to your question here is that Rambam was openly disputing the view of the Sages, and was not overwhelmed by that. Indeed, there are examples in Rambam’s writings where he clearly rejects the views of some Sages on philosophical grounds. For instance, in Guide for the Perplexed 3:31 Rambam ridicules the view that the commandments have no reasons, but in 3:48 he acknowledges that that is the view of the Mishnah.
THERE are persons who find it difficult to give a reason for any of the commandments, and consider it right to assume that the commandments and prohibitions have no rational basis whatever. They are led to adopt this theory by a certain disease in their soul, the existence of which they perceive, but which they are unable to discuss or to describe.
When in the Talmud (Ber. p. 33b) those are blamed who use in their prayer the phrase, "Thy mercy extendeth to young birds," it is the expression of the one of the two opinions mentioned by us, namely, that the precepts of the Law have no other reason but the Divine will. We follow the other opinion.
Furthermore, the question of what actually happened in this incident is ultimately a historical question. Rambam elsewhere disagrees with the Sages in matters of historical occurrence. For example, the Talmud (Berachot 28a) states:
ההוא יומא בר תמני סרי שני הוה אתרחיש ליה ניסא ואהדרו ליה תמני סרי דרי חיורתא היינו דקאמר ר' אלעזר בן עזריה הרי אני כבן שבעים שנה ולא בן שבעים שנה
He was eighteen years old that day, and a miracle was wrought for him and eighteen rows of hair [on his beard] turned white. That is why R. Eleazar b. Azariah said: Behold I am about seventy years old, and he did not say [simply] seventy years old.
Rambam, however, in his commentary to the Mishnah that is cited here in the Talmud, gives a different explanation in which R. Elazar Ben Azariah's beard did not miraculously turn white.
Indeed, R. Solomon Luria in his commentary to Sanhedrin 52b argues with an amora as to the historical reality of what another amora's reasoning for a specific action was. He defends this by noting that it is permissible to dispute the Sages in matters that don't pertain to law:
ואינני כחולק על התלמוד שהרי אין בו נפקותא לא לחיובא ולא לפטורא ואין זה אלא למשיחא
And I am not like one who disagrees with the Talmud, for there is no relevance to obligation or exemption, and it is only for Messiah.
Interestingly, in Rambam’s commentary to the Mishnah in Avot he explains that the point is that miracles were programmed into nature from the outset, and he mentions the mouth of the donkey without saying that it didn’t actually happen in reality.