Keep in mind that the Psalms comprise the music of the redeemed, the people of God. Among the many things they express is the notion that God’s people are favored (by God) above all other people.
The writer of Psalm 18 was in dire straits and if his position were not weak enough, his enemies used the occasion of his vulnerability to attack him. But God heard the prayer of his servant and in righteous indignation “thundered from heaven.” The enemies of the Psalmist were the enemies of the Lord, and God rescued his servant.
The Psalmist writes that God dealt with him according to the writer’s righteousness. One should not read that as saying God rescued him because he was good and deserved rescue. He simply wants the reader to know that the misfortune that befell him was not punishment for sin. In other words, God did not rescue him from what he deserved. In the end, God rescued him because he was God’s anointed (vs. 50).
Christians should remember they have a special relationship with God above all other people. We should live in a way that appreciates that exalted status, and trusts that because of it, God will come to our aid in times of trouble.