The Gemara (Ta’anit 7a) compares the Torah to water, going so far as to explain the story of Mei Marah (Shemot 15:22-27) based on this comparison (Bava Kama 82a). Chazal explain that when the Torah states that Bnei Yisrael could not find water for three days, it is a reference to them having not learned Torah for three days. It is difficult not to recognize the similarity, even in a most superficial way, between the stories of Mei Marah in BeShalach and Mei Merivah in Chukat (BeMidbar 20:3-13). However, the comparison between these two stories’ contexts is even sharper. Prior to these two instances, Miryam, who scarcely appears in the Torah, is mentioned. Prior to the episode Mei Marah, Miriam is found leading the women in their own Shirah just as the men had done (Shemot 15: 20-21). Immediately before Mei Merivah, the Torah records the death of Miriam, leading to the interpretation of Chazal (Ta’anit 9a) that due to the juxtaposition between the death of Miriam and Bnei Yisrael’s complaint regarding a lack of water, it is clear that it was in Miryam’s merit that the well provided water for Bnei Yisrael throughout their sojourning in the Midbar. Furthermore, Bnei Yisrael play a significant role in the contextual makeup of these stories as in BeShalach (Shemot 15:1) and Chukat (BeMidbar 21:17), Bnei Yisrael sing in unison (both times using the language, “Az Yashir,” “Then they sang”), both relating to miracles involving water; the former being Keri’at Yam Suf and the latter being Be’eir Miryam. This very similar parallel forces us to see the greater lesson being highlighted by the Torah by juxtaposing the stories of Miryam’a death, Bnei Yisrael’s complaining about the lack of water, and Bnei Yisrael’s Shirah.
The Or HaChaim (BeMidbar 21:17 s.v. Az Yashir Yisra’eil) asks why Bnei Yisrael sang a joyous praise about the water provided for them in the Midbar rather than the Man which satiated them. He answers, based on the aforementioned Ma’amarei Chazal, that Bnei Yisrael were not singing about water, but rather the Torah, which is compared to it. In a similar vein to how Chazal interpreted Mei Marah, the Or HaChaim seeks to explain this spontaneous Shirah of Bnei Yisrael. The question remains, though, why Bnei Yisrael choose to celebrate the Torah at this point, nearly forty years following its acceptance. The Netivot Shalom in Parashat BeShalach explains that the spontaneous Shirat HaYam represents the ideological shift of Bnei Yisrael; they finally understood that all of the oppression, slavery, and ruthlessness they had experienced was for the good. The idea of Shirah represents a complete submission to Hashem, recognizing that He does what is best for us.
Rashi (21:20 s.v. UMiBamot) points out Moshe’s absence from the Shirah in Chukat, attributing it to Moshe’s sin regarding the rock (20:9-13). However, it is possible to explain Rashi based on the aforementioned idea. It is not Moshe’s sin per se which removes his name from this Shirah, but Moshe’s resulting begging to Hashem to enable him to enter Eretz Yisrael before his death (Devarim 3:23-25). At this point, Moshe had not yet realized the benefit which could arise as a result of him being restricted from Eretz Yisrael. Hashem, however, ensures him that he will merit even greater things following his death (Rashi 3:26 s.v. Rav Lach). Hashem needed to ensure Moshe that despite this setback, everything would be good. It is for this reason that Moshe is absent from the Shirah in Chukat. The reaction which resulted from the conflict with the rock clearly represented a lack of full understanding of Hashem’s goodness and fairness. Therefore, while Bnei Yisrael graciously thanked Hashem for providing them with the Be’eir Miryam, Moshe was unable to bring himself to sing with them.
We can now explain the presence of Miryam in both stories, as well the complaints of Bnei Yisrael which follow. Miryam is the epitome of a Tzadeket as our introduction to her in the Torah represents her full compliance with the Ratzon Hashem. It is Miriam (Shemot 2:4) who watches her baby brother Moshe flow down the river in order to save his life. This scene, the one which introduces Miryam to our attention, establishes her as someone who understands the concept of, “Gam Zu LeTovah,” that everything is for the good. Despite having to leave her brother, unaware of what might happen to him, she understands that Hashem has a plan and that whatever He chooses will be right. It is for this reason that Bnei Yisrael begin to complain following Miryam’s mention in the Torah. As Chazal explain, their complaint by Mei Marah was not regarding water, but Torah. When Bnei Yisrael saw Miryam, whose merit warranted the supply of water to Bnei Yisrael, singing and dancing following Keri’at Yam Suf, they realized that they had not truly reached a level of understanding that all of the affliction Hashem had brought upon them was for the good. It is true – Bnei Yisrael spontaneously sang in gratitude to Hashem, but they understood that they were nowhere near the level of Miryam, the true essence of the understanding that Hashem does only what is beneficial to us. Therefore, after seeing Miryam, Bnei Yisrael feel as if they have not learned Torah; they were thirsty, craving a sip of Torah, a sip of understanding Hashem’s ways. Miryam exemplified for them a true understanding of Hashem and His ways. Bnei Yisrael needed to learn more Torah, to learn Hashem’s guidebook and secrets in order to come to a better understanding.
Regarding Mei Merivah, Bnei Yisrael had a similar reaction. Miriam had died, and Bnei Yisrael saw that they no longer had a figurehead to exemplify the true notion of Hashem’s complete and utter goodness to His people. Upon this realization, they once again complained about the lack of “water.” There is a stark contrast, though, between the complaining here and that in BeShalach: here, they sang following the complaining; there, they sang prior. In BeShalach, Bnei Yisrael reached the high level of understanding Hashem’s goodness. It was only after seeing Miryam that they realized they did not have a proper understanding of Hashem. However, following the complaining in Chukat, they finally realized that, even without Miriam, they could appreciate all of the good Hashem had done for them in the Midbar. After Hashem responded to their complaints by providing water, something had clicked in the minds of Bnei Yisrael. Perhaps they drew inspiration from Miriam’s death and honored her by understanding what she truly represented. It is no coincidence that the Shirah in Chukat effectively ends the long string of complaints that had become the bulk of the past few Parashiyot. Through Miryam and Bnei Yisrael’s Shirah in Chukat, we may to learn the essence of Hashem’s goodness, and that despite hardship, Hashem enables us to succeed.
 See Berachot 7a for proof of this.