The message here is that God executes the wicked by their own devices; in this case, by the sword.
"Rashi paraphrases this same Midrash again on the verse:``and Balaam the son of Beor they slew with the sword"":."