The first stanza of Ma'oz Tzur is an introduction to the song, the next four stanzas record historical events where G-d saved the Jewish people, and the sixth and last stanza is a prayer for the future redemption. This last stanza has several versions, and is deleted altogether in some versions (see for example in Hertz, 1952, p. 952).
Why is the last stanza deleted in some versions? A possible answer is that the stanza has a call for vengeance, but Yom Tov Levinsky (1956, vol. 5, pp. 180-182) notes that there are other prayers (for example, Av ha-Rachamim) that also have calls for vengeance but these have not been altered or removed. Levinsky claims that the problem with the sixth stanza is the last line of the stanza, dehei admon be-zel zalmon, which he claims refers to Fredrick Barbarossa, a German King who was a redhead (admon) and one of the leaders of the Third Crusade, 12th century. According to Levinsky, the problem with the line is that it called for Fredrick Barbarossa to be vanquished.
Ismar Schorsch (1988) completely rejects this possibility. He notes that the stanza is not cited until the seventeenth century long after the time of Fredrick Barbarossa. Instead, he claims that the stanza is referring to the time after the Reformation, that the Jews were hoping that this would signal the end of Christianity. And, since Christianity was identified with Edom, the last of the four empires that would rule before the Messiah, the stanza is a prayer for the coming of the Messiah. He interprets the line dehei admon be-zel zalmon, as a prayer to vanquish Christianity (admon = Edom, and zalmon = zelem, the cross). Furthermore, he notes that the final verse mentions seven shepherds, which refers to seven biblical figures (Micah 5:4 and Succah 52b) who would accompany the messiah.
Why was the custom of singing Ma'oz Tzur established? Is it just because the song is a nice song? Why do we not sing Ma'oz Tzur on Purim and Pesach, as the song also refers to these times of salvation?
Avigdor Shinan (1999, p. 98) writes that it seems that the song was written due to the persecutions experienced by German Jewry in the 13th century. The idea being that the song would have been relevant to German Jewry in the 13th century, who suffered greatly from persecutions, as the song was (is) a prayer that G-d would save the people as He had in the past. With this reason the song is not specifically related to Hanukkah and to candle lighting on Hanukkah.
Another answer can be derived from Rav Soloveitchik's (2007, pp. 167-171) discussion of some aspects of the Rambam's comments on Hanukkah. The Rambam (Laws of Hanukkah, 3:1) begins his discussion of the laws of Hanukkah with a review of the history of Hanukkah and the question is why. The simple answer is that since the events of Hanukkah are not recorded in the Bible, the Rambam does not assume that people know the events of Hanukkah. However, still the question remains why do people need to know the events of Hanukkah?
Rav Soloveitchik notes that the Rambam (Laws of Hanukkah 4:12) writes that the lighting of the candles is "in order to make known the miracle and to offer additional praise and thanksgiving to G-d for the wonders which he has wrought for us." Rav Soloveitchik explains that there are two aspects to lighting the candles on Hanukkah, the technical aspect of physically lighting the candle and the thoughts that are to accompany the lighting of the candles. Based on the Rambam's comments, these thoughts are to thank G-d for the miracles he has done for us. Furthermore in order for one to be truly thankful, one must know what one is thanking G-d for, and hence the Rambam recorded the history of Hanukkah so that people would know what they are to be thankful for when lighting the candles. Rav Soloveitchik notes that the same idea applies to the Haggadah on Pesach. One must thank G-d for taking the Jewish people out of Egypt, and hence one must review the history of the Exodus. Another example is the prayer al ha-nissim that we recite on Purim and Hanukkah. We do not just say that we are thankful but we recite the history of the different holidays in order to know what we are thankful for.
I did not see that Rav Soloveitchik discussed Ma'oz Tzur, but his idea could apply to why the custom of singing this song began. Maybe the author of the song, Mordechai, wrote this song in order that people would understand the significance to lighting the Hanukkah candles that people are to acknowledge and be grateful that G-d saved the Jewish people in the times of the Hasmoneans. This idea that G-d saved the Jewish people is expressed in the prayer al ha-nissim, but that prayer does not refer to the miracle of the oil and is not said when one lights the candles. Similarly, the piyyut ha-nerot ha-lalu refers to G-d doing miracles for the Jewish people, but also does not specify about the miracle of the oil and does not explain that the people were in dire straits prior to the miracles. Ma'oz Tzur specifically mentions both the miracle of the oil and how difficult the situation was for the Jews prior to G-d's help. Thus, maybe the reason why the custom of singing Ma'oz Tzur was adopted was because the song is way of teaching people that when one lights the Hanukkah candles one should be thankful to G-d for saving the Jewish people. (With this logic, the other stanzas not referring to Hanukkah were added to give more examples where G-d saved the Jewish people. Furthermore, as noted by daughter Talia, with this logic there would be no need to recite Ma'oz Tzur on Purim where we read the Megillah or on Pesach where we recite the Haggadah.)
A different possibility is that the Rama (Shulchan Arukh, Orah Chayyim 670:2) writes that the custom is to recite zemirot and praises by the meals on Hanukkah and then the meals are considered as being commanded meals, seudat mitzvah. What are these zemirot? Zemirot are songs, but what songs are being referred to? Maybe Ma'oz Tzur, which has a similar form to the zemirot we sing on Shabbat, was written specifically to have a song to sing by the meals on Hanukkah in order to transform a meal on Hanukkah from a regular meal to a seudat mitzvah. With this idea, at some point the song was transferred from being part of the meal to the ceremony by the lighting of the Hanukkah candles. This rationale would also explain why there is no need to recite Ma'oz Tzur on Purim or Pesach since the meals on these holidays are suedot mitzvoth, obligatory meals, which removes the need to add zemirot to the meals.
Hertz, Joseph H. (1872-1946), 1952, The authorised daily prayer book, revised edition, New York: Bloch Publishing Company.
Levinsky, Yom Tov, 1956, Sefer ha-Moadim, Tel Aviv: Dvir.
Millgram, Abraham, 1971, Jewish Worship, Philadelphia: The Jewish Publication Society.
Schorsch, Ismar, 1998, A Meditation on Maoz Tzur, Judaism, 37:4, pp. 459-464.
Shinan, Avigdor, 1999, Siddur Avi Chai for the house and family, Jerusalem: Yidiyot Ahranot and Sefer Hemed.
Soloveitchik, Joseph, (1903-1993), 2007, Days of Deliverance: Essays on Purim and Hanukkah, edited by Eli D. Clark, Joel B. Wolowelsky and Reuven Ziegler, Jersey City, NJ: Ktav Publishing House.
Millgram, Abraham, 1971, Jewish Worship, Philadelphia: The Jewish Publication Society.
Schorsch, Ismar, 1998, A Meditation on Maoz Tzur, Judaism, 37:4, pp. 459-464.
Shinan, Avigdor, 1999, Siddur Avi Chai for the house and family, Jerusalem: Yidiyot Ahranot and Sefer Hemed.
Soloveitchik, Joseph, (1903-1993), 2007, Days of Deliverance: Essays on Purim and Hanukkah, edited by Eli D. Clark, Joel B. Wolowelsky and Reuven Ziegler, Jersey City, NJ: Ktav Publishing House.