This seemingly vicious practice stems from the Lord's instruction to the Israelites when encountering the many nations inhabiting the promised land. The Lord commands that they were to "smite them, and utterly destroy them" (Deut. 7:1-2). Compliant with this command, on one occasion Joshua and the Israelite army "utterly destroyed" Jericho and its inhabitants: "And they utterly destroyed all that was in the city, both man and woman, young and old, and ox, and sheep, and ass, with the edge of the sword" (Josh. 6:21). Later, other cities, such as Makkedeh, Libnah, Laschish, Eglon, Hebron, and Debir, were "utterly destroyed . . . ., and all the souls that were therein" (Josh. 10:28, 32, 35, 37, 39). Sounds pretty brutal, right?
In Hebrew, the lexical root for "utterly destroyed," as translated in the KJV, is h-r-m, or herem. The word herem is most often translated as "accursed," and means to be devoted or set apart. This accords with Joshua's instruction to the Israelite armey prior to attacking Jericho: "[T]he city shall be accursed, even it, and all that are therein . . . . And ye, in any wise keep yourselves from the accursed thing, lest ye make yourselves accursed, when ye take of the accursed thing" (Josh. 6:17-18). Consequently, through its "utter destruction," a city and its people were symbolically being set apart for or otherwise devoted to God. The precious metals found in the cities, however, were not destroyed but were instead purified and placed in "the treasury of the Lord" (Josh. 6:19, 24).
The Israelite's were fighting their king's war, and as such the spoils of the battle rightfully belonged to Jehovah. Therefore, Jehovah sometimes required that his people devote everything to him by "utterly destroying" entire cities and populations. This is not unlike ritual killing and Israelite animal sacrifice, where the burnt offering is entirely consumed, or devoted to God, on the sacrificial altar. Likewise, after overrunning a city and killing all of its inhabitants, burning the city with fire made it a large sacrificial offering devoted to the Lord.
One of the more well-known stories applying herem is recounted in 1 Samuel 15, where Saul is instructed to "utterly destroy" the Amalekites, including all of their people and animals (1 Sam. 15:3). Contrary to Samuel's instructions received from the Lord, Saul returned with Agag the Amalekite king and "the best of the sheep, and of the oxen, and of the fatlings, and the lambs, and all that was good, and would not utterly destroy them: but every thing that was vile and refuse, that they destroyed utterly" (1 Sam. 15:9). Agag was apparently a trophy of Saul's victory over the Amalekites, and it was Saul's intent to sacrifice the captured livestock to the Lord as a burnt offering. This, however, is not the intent of herem, so Samuel was required to finish the job by "hew[ing] Agag in pieces before the Lord"(1Sam. 15:33).
Although a seemingly vicious act, the requirement of the Lord that the Israelites "utterly destroy" cities and their inhabitants was actually a form of worship and proved one's devotion to Jehovah.