Rav Chaim Volozhin makes a telling observation in his Ruach Chaim on Pirkei Avos 1:1 (see here) (the left column, 7 lines up from the bottom).
ולכן ניתנה התורה לו במתנה (כמבוא' בנדרים לח:) "בתחילה היה משה למד תורה ומשכח עד שניתנה לו במתנה כו'" וכאמחז"ל בעירובין נד ״וממדבר מתנה״, אם משים אדם עצמו כמדבר זה שהכל דשין בו ואין מקפיד כלל על כבודו התורה ניתנה לו במתנה ולכן לא היה משה שוכח שום דבר מהתורה יען שלא הקפיד על כבודו כלל ונתנה לו
And therefore, the Torah was given as a gift (like it explains in Nedarim 38) "Initially, Moshe would study Torah and forget it, until it was given to him as a gift etc." - and it is like Chazal said in Eiruvin 54a "And from the wilderness they went to Mattanah” (Numbers 21:18). If a person makes himself (humble) like this wilderness, which is open to all and upon which everyone treads, (his Torah study will endure and be given to him as a gift [mattana])". And since he (Moshe) was not at all particular about his honor, the Torah was given to him as a gift. And therefore, Moshe did not forget a thing because he was not particular about his own honour and so it was given to him.
So according to Rav Chaim, as per the gemara in Eiruvin, Torah is acquired through humility. I don't know if we can assume that Moshe was not humble and then acted accordingly and he then retained his Torah. However, we know that the Torah calls him the humblest person that ever lived (see Bamidbar 12:3). So perhaps when he channelled this humility it made him into a more fitting receptacle, and he was thus able to remember it all as he was operating on the requisite level to receive Torah.
Some further digging came up with this piece from Rabbi A. Leib Scheinbaum of Hebrew Academy of Cleveland here.
He begins by noting your question and strengthens it by asking why the need to spend so long on Har Sinai, if he was only to forget it:
Moshe Rabbeinu remained on Har Sinai for forty days and nights, while Hashem taught him the entire Torah. In the Talmud Nedarim 38a, Chazal tell us that, despite Moshe's outstanding acumen and memory, "he learned and forgot, learned and forgot." In other words, he could not retain the Torah lessons that he was receiving from Hashem. In truth, this is not surprising. How could a human being comprehend the wisdom of the Creator? This troubled Moshe as he became increasingly frustrated with his inability to absorb and retain his knowledge of the Torah. In the end, Hashem gave him his Torah knowledge as a gift. We wonder why it had to be this way. Hashem knew that a yelud ishah, human being, was incapable of absorbing the entire Torah, and, ultimately, the only way he would grasp it would be as a gift. Why did Hashem make Moshe spend forty days on the mountain working at a task that was impossible and would inevitably end in failure?
He answers that since Moshe would be the teacher of klal yisroel he would have to experience the sense of toil and hard work that is needed to master Torah. In other words, the forgetting and trying again was a trait that was crucial to his role, and it was the means through which he earnt this gift:
The Alshich HaKadosh explains that Hashem selected Moshe as the one to receive and eventually transmit the Torah to Klal Yisrael. He would be the source from which future generations of Jews would learn Torah. To be worthy of this monumental privilege, Moshe would have to sustain the emotional pain and frustration of "learning and forgetting" the Torah that he had been taught for forty days. Horav Avraham Pam, zl, notes, as cited by Rabbi Sholom Smith, in his anthology on Chumash, that Moshe's frustration was probably exacerbated by the realization that he would only be able to transmit to Klal Yisrael that which he remembered. Whatever he forgot would be lost to eternity. Yet, Moshe persevered. His desire to absorb the eternal verities and wisdom of the Torah catalyzed him to go on, not to give up. Thus, he earned the Divine "gift" of the Torah.
Moshe earned the privilege to be the quintessential Torah leader, relaying the Torah in its entirety, even the profound insights that every perceptive student in the future would innovate, only because he overcame his own frustration, his feelings that his efforts were nothing more than an exercise in futility.