But isn’t it written: “The fathers shall not be put to death for the children (nor shall sons be put to death because of fathers; each man shall be put to death for his own transgression.)"(Deuteronomy 24:16)? Rabbi Ḥiyya bar Abba said that Rabbi Yoḥanan said: It is better that one letter and one mitzva be uprooted from the Torah in this manner and thereby the name of Heaven not be desecrated in public.
Rabbi Yoḥanan said in the name of Rabbi Shimon ben Yehotzadak: It is better that one letter be uprooted from the Torah and thereby the name of Heaven be sanctified in public. How so? As the Gentile passersby would say: What is the nature of these people who have been left hanging here for so long? They were told that these are sons of kings. And what did they do to deserve such a fate? They had laid their hands upon and caused harm to calculating converts who had converted for personal gain and were never permitted to enter into the congregation. Those passersby said: There is no nation as worthy of cleaving to it as this one. If the sons of kings who harmed converts are treated in this manner, all the more so would the sons of ordinary people be. And if calculating converts are related to in this way, all the more so would this apply to members of the Jewish people themselves.
Immediately, one hundred and fifty thousand converts joined the Jewish people,...
(excerpts Talmud Yevamos 79a , translation from Sefaria)
A regular Jewish court would not be allowed to kill sons for fathers. However, this command came from G-d as a blatant exception to the rule. Since it was a miraculous decree from heaven directly ordered, it was carried out as a Horaah HaSha'ah (one time decree).
Apparently, the lesson we learn is that oppressing the weak and lowly is so evil in the eyes of G-d that it may override the regular course of action (as in this case).