Sometimes, the penalty-statement is a maximum, and not a mandatory penalty. The task of law-interpretation and law-application was one of wisdom, and not a job of accurate third-grade reading skills.
In the most obvious of rape-cases, the criminal was liable to death, see Dt.22:25. But, on the other hand, if the story was he-said/she-said and the case was inconclusive, there was a way to obtain a modicum of justice in a murky situation.
If God--who knows all things--was willing for those children to perish, he had a sufficient moral-justification for it. He did not always so command it; in fact, he made provision at times for mercy. Oddly (to our sentiments, at times) sometimes death (and swift at that) is mercy. Frankly, if an Israelite just left the city and its dead, with a few orphans wandering around in the desolation (hard to bear the thought, yes?) all would likely be dead soon anyway; or enslaved by some other party.
Suppose a case where all able-bodied persons are combatant, and all such persons are judged of God as irreparably corrupt. Stipulated: all of these adults deserve to die, and the warfare is just. Whose responsibility is it to care for their offspring? The Israelites? Why? If at times, when permitted of God, some of these were spared and taken care of by Israelites (not the case in e.g. 1Sam.15:3), this too was mercy; and, importantly, the kindness was not deserved.
We tend to gauge conditions in an ancient setting--or even in some other, rougher part of the world today--by our local standards: be that in sanitation, cuisine, justice, warfare, servitude, etc. We tend to like those parts of our own system that seem to have some sanction of God by loose conformity with our interpretation of the ancient world as we engage with it in the pages of the Bible. We are quick to make absolute connections with more than the moral-law, as if that were a simple task, and unconnected with time or place.
Life was (and sometimes still is) nasty, brutish, and short; mainly on account of sin and a resultant reduction of civility to a minimum. Bridges between societies are notoriously difficult to build and maintain; however much of an ideal it may be. The Ancient Near East was a place without much civility. Societies kept their standards and law within them; but beyond them was a world of conflict. "What's mine is mine; what's yours is negotiable."
Such was the chaos into which God saw fit to introduce a new nation, a covenanted people; while at the same time he judged the Canaanite society to be unfit to remain in the world. God personally had brought more, and more complete, ruin on an entire world--of men, women, and children--only a few generations prior by a flood. Drowning is a frightful and painful way for anyone to die.
God takes numerous children out of this life, who "have no knowledge of right from wrong." Is he evil or unjust for it? If we (rightly) say "No," then if he determined for Israel to be his instrument at one time in world history, he does not need any more justification than his commandment. I have proposed some rationalization in the above commentary, but do not mistake that for theodicy. God needs no such rational defense for his morally perfect will.