There is a mitzvah to follow the rules of a milchemes r’shus, that is, a war fought voluntarily (as opposed to a war of conquest required by the Torah). The rules of optional wars include making an offer of peace. The cities to be attacked were to be offered the option of agreeing to be subjects of the king of Israel with all that entails (such as paying taxes). If war was the choice, only males who had reached the age of majority were to be considered combatants; women and children could be captured but they were not to be killed. Also, an escape route was to be left open, in case the enemy chose to flee.
The rationale behind this mitzvah is that mercy is a desirable trait to cultivate, even in war. (Perhaps especially in war!) It behooves us to treat all mankind with mercy, even if they are idolators or enemies in war. Wanton destruction in the name of conquest would do the opposite, cultivating the trait of cruelty.
This mitzvah applies when the Jews occupy their land. It is discussed in the Talmud in tractate Sotah on page 44b. It is codified in the Mishneh Torah in the sixth chapter of Hilchos Melachim. This mitzvah is #190 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos.