In the very moment in which you feel that the temptation is strongest and that you are least prepared for prayer, go off into a solitary place (Matt. 6:6), and pray the Lord’s Prayer or whatever you can say against Satan and his temptation. Then you will feel the temptation subsiding and Satan turning tail. If anyone thinks that prayer should be put off until the mind is clean of impure thoughts, he is doing nothing but using his wisdom and strength to help Satan, who is already more than strong enough. This is really heathen and sophistic religion, the very teaching of Satan. Against it we must maintain the example and teaching of this psalm [Psalm 51], where we see that David, viewing his total impurity and his special sin of the flesh, does not flee from God, the way Peter foolishly said in the ship (Luke 5:8), “Depart from me, for I am a sinful man, O Lord.” But with trust in mercy he breaks out in prayer and says, “Lord, if I am a sinner, just as I am, have mercy on me.” Just because our hearts really feel sin, we ought to come to God through prayer all the more.
Martin Luther commenting on Psalm 51:1
Luther’s Works v. 12: Selection from the Psalms 318