Shemot 11:5 records that Moshe warned Pharaoh that in the tenth plague all of the firstborn Egyptians including the firstborn Egyptian animals would die, and Shemot 12:29 records the fulfillment of this plague. Why were these people and animals killed? Was there a need for the tenth plague?
The tenth plague was not necessary to free the Jewish people from slavery since G-d could have continued each of the nine plagues until the people left Egypt. For example, during the plague of darkness, the people could have walked out when the Egyptians could not see anything. Or, G-d could have continued the plague of locusts until the people left. However, G-d always stopped the plagues without freeing the people and then after a plague ended, Pharaoh refused to let the people go. What was the reason for the tenth plague?
The tenth plague was a punishment for the Egyptians killing the male Jewish children, Shemot 1:22 (see Seforno on Shemot 8:12), and we see from the story of Moshe being hidden in the reeds of the Nile that Pharaoh’s decree to kill the male Jewish children was implemented, Shemot 2:1-6. This punishment accords with the verse in Bereshit 9:6 that people who murder are supposed to be killed. In addition, the Egyptians severely mistreated the Jews without any order from Pharaoh because they loathed the Jewish people, Shemot 1:12-14, see our discussion on Shemot 1:7-22, "Population dynamics" (https://lobashamayim.blogspot.com/2009/01/shemot-18-22-population-dynamics.html).
If all the Egyptians (except Pharaoh’s daughter and the midwives(?)) killed the Jewish male children, why was tenth plague limited to the firstborn male Egyptians? Bekhor Shor (on Shemot 4:23, quoted by Hizkuni on 4:23) writes that the tenth plague was a measure for measure, midah keneged midah, since Shemot 4:22 refers to the Jewish people as the firstborn of G-d. Yet, for some period of time all the Jewish male children were killed by the Egyptians, and then a punishment that was measure for measure would have been that all the Egyptian males should have been killed.
Rashi (on Shemot 11:5) tries to explain why the firstborn non-Jewish slaves, the firstborn of the poor Egyptians and the firstborn animals were killed. He writes that the firstborn non-Jewish slaves were killed since they should not say that their gods had caused the plague and the firstborn of the poor Egyptians were killed since they too had made the Jews slaves. This latter explanation accords with our understanding of Shemot 1:12-14, but there is no reason to assume that the poor Egyptians and non-Jewish slaves did not participate in the murder of the Jewish male children. With regard to the firstborn animals, Rashi writes that they were killed since these animals had been worshipped.
My understanding is that based on Bereshit 9:6 all the Egyptian men should have been killed in the tenth plague since the Egyptians killed the male Jewish children, and this would follow the idea of a punishment being measure for measure. However, the punishment was limited to just the firstborn male Egyptians to reduce the number of people who were punished. Why then were the Egyptian firstborn males killed as opposed to some other criteria as for example, the youngest child in each family? My guess is that the firstborn males were selected since this would establish the law of the firstborn males by the Jewish people that they are kadosh, Shemot 13:1,12-15, independent of the fact that they are born first, as from the book of Bereshit we see that the Torah does not believe that being born first is a reason for a person to receive special privileges. This idea also explains why the firstborn animals were killed in the tenth plague since they too were killed to generate the source of the future law of the kedusha of the firstborn animals.