A psalm of David is inserted here, several chapters after his last entry. David writes this psalm in response to Nathan’s rebuke after David had gone in to Bathsheba and had Uriah killed to cover up his sin. David begins by asking God for mercy, that He would blot out his transgressions and wash him clean. He then acknowledges his sin and declares that only against God has he sinned. He recounts that he was brought forth (born) in iniquity, and God desires truth in the inward parts. He then speaks the famous words of his prayer.
Psalm 51:7–13 (NKJV)
7 Purge me with hyssop, and I shall be clean;
Wash me, and I shall be whiter than snow.
8 Make me hear joy and gladness,
That the bones You have broken may rejoice.
9 Hide Your face from my sins,
And blot out all my iniquities.
10 Create in me a clean heart, O God,
And renew a steadfast spirit within me.
11 Do not cast me away from Your presence,
And do not take Your Holy Spirit from me.
12 Restore to me the joy of Your salvation,
And uphold me by Your generous Spirit.
13 Then I will teach transgressors Your ways,
And sinners shall be converted to You.
He then goes on to ask God for forgiveness, and pledges to praise God. He declares that God is desirous of brokenness, rather than sacrifices.
David writes in response to a heinous pattern of sin in the matter of Bathsheba. He is described as a man after God’s own heart, and it is written that he did not sin against God in anything except the matter of Bathsheba. So this sin is an anomaly, a swift, but brief deviation from the path of devotion and care with which David worshipped God. This severe departure from obedience is so out of character, yet his response to the conviction of the Holy Spirit is completely in character for the shepherd king. David fell on the mercy of the true King and pled to be restored. This response to conviction is a pattern which we should follow in our time of sin. God is holy and must be obeyed. However, when we fail and conviction falls, it is imperative to repent and ask God for forgiveness. He will take back that which has been separated by sin.