What does Proverbs 21 mean when it says The wicked shall be a ransom for the righteous?

What is the meaning of Proverbs 21:18, “The wicked shall be a ransom for the righteous, and the transgressor for the upright”?
Compare the following passages, in which a similar thought is more clearly expressed: “The righteous is delivered out of trouble, and the wicked cometh in his stead” (Proverbs 11: 8).
“I gave Egypt for thy ransom, . . . I have loved thee, therefore will I give men for thee and people for thy life” (Isaiah 43:3, 4).
“They hanged Haman on the gallows that he had prepared for Mordecai” (Esther 7:10).
“Those men which had accused Daniel. . . cast. . . into the den of lions” (Daniel 6:24).
And note the use of the same word “ransom” in the Isaiah passage, and of “in his stead” in Proverbs 11.
The above Scriptures, and others which might be added, suggest two things. First, that God may sometimes deliver His own at the cost of severe suffering, or even destruction, to the unsaved. Second, that the wicked at times suffer the very fate which they themselves had prepared for the righteous. Proverbs 21:18 contains the first of these thoughts, and probably the second as well.