OK the answer is not an easy one to accept, but it is the reason why God allowed them, or had them kill those children. First I'm going to tell you the general reason why they're suffering, and then the specific reason why I those children suffered such horrific deaths.
1st, the Nation of Israel is undergoing judgment because of their chronic rebellion against Him. Not only that they were following after other gods but the religious practices that they've incorporated includes burning alive their own children for prosperity (Psalms 106:37).
2nd the children in how they've behaived are often the product of their parents whether they're taught wrongly or that the children grew up in a chaotic home.
In any case, one of the things that children cannot do is to disrepect thier parents; the 6th Commandment "Honor your Father and Mother", must be done and if the parents despite their best efforts, could not correct their child they're to bring him to the elders and if approved, the child is killed. Yes, they took that commandment seriously (Deuteronomy 21:18-21),
Elisha was not a parent to those children, however, apart from the fact that a king should not be disrepsected, so also the prophet, and Elisha was that Prophet. He is also well-known as he was a student of the great Elijah.
Children as you know can be very vicious and so when the 40 mocked him, not only that should not be done but that many children in my mind is indicative of just how wiclked the community was.
Even if the community is not any worse than the others, Israel is under the judgment of God because of the many sins committed by them; and the community that included the 40 were mostly likely as guilty of the chronic sins the rest committed..