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.
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.