"Wickedly" is an adverb in that sentence. It means that turning away from God would have been the wicked thing to do. Young's Literal Translation reads a little better.
"For I have kept the ways of Jehovah, And have not done wickedly against my God." (Psa. 18:21, YLT)
Brown-Driver-Brigs has the first definition of "rasha" as "1. be wicked, act wickedly....(in departing) from my God,..." Source: Biblehub
It is not speaking of how we turn away from God, but that we turn away from Him. In other words, to turn away from God is a wicked thing to do.
The background of this Psalm is of David's deliverance from the evil at the hand of Saul (vs.1), so possibly before David's sins with Bathsheba. But, there is the possibility that this Psalm is attributed to the Messiah.
Excerpt from Adam Clarke's Commentary:
"The reader is requested to turn to the notes on 2 Samuel 22:1, for some curious information on this Psalm, particularly what is extracted from Dr. Kennicott. This learned writer supposes the whole to be a song of the Messiah, and divides it into five parts, which he thus introduces: -
"The Messiah's sublime thanksgivings, composed by David when his wars were at an end, towards the conclusion of his life. And in this sacred song the goodness of God is celebrated,
For Messiah's resurrection from the dead, with the wonders attending that awful event, and soon following it.
For the punishment inflicted on the Jews; particularly by the destruction of Jerusalem. And,
For the obedience of the Gentile nations. See Romans 15:9; Hebrews 2:13; and Matthew 28:2-4; with Matthew 24:7, Matthew 24:29."
And that the title now prefixed to this hymn here and in 2 Samuel 22:1, describes only the time of its composition, seems evident; for who can ascribe to David himself as the subject, 2 Samuel 22:5, 2 Samuel 22:6, 2 Samuel 22:8-17, 2 Samuel 22:21-26, 2 Samuel 22:30, 2 Samuel 22:42, 2 Samuel 22:44, etc.?
In Dr. Kennicott's remarks there is a new translation of the whole Psalm, p. 178, etc.
The strong current of commentators and critics apply this Psalm to Christ; and to oppose a whole host of both ancients and moderns would argue great self-confidence. In the main I am of the same mind; and on this principle chiefly I shall proceed to its illustration; still however considering that there are many things in it which concern David, and him only." Source: StudyLight
All turning away from God is sin and is wicked. And, whether we acknowledge it or not, we are responsible for that sin. It is our choice. It does not imply that we cannot repent of that sin and ask for forgiveness.