Every word has a form - what it looks like - and a function - what it does.

The word 'so' generally functions as an adverb, but it can have other functions. In the example below, 'so'
refers back to 'loyal to her friends' which makes it pronominal in function.
She was
loyal to her friends and she remained
loyal to her friends.
She was
loyal to her friends and she remained
so.
She was
loyal to her friends and she remained
that way.
'so' functions as a nominal but it still expresses an adverbial meaning. Compare:
[1] She was
loyal to her friends and she remained
it. (
awkward)
[2] She was
loyal to her friends and she remained
so. (i.e., that way)
The first example is awkward with 'it' because 'it' refers back to an adjective phrase: loyal to her friends. Pronouns refer back to nouns, right? Never adjectives.
The second example is grammatical because 'so', an adverb, refers back to an adjective phrase. Note, adverbs modify verbs and adjectives.
'so', an adverb by nature, modifies adjectives. That's something the pronoun 'it' cannot do.
In short,
doing it: 'it' is a nominal, so it must refer back to a nominal
doing so: 'so' is an adverb. It works with verb(al)s and adjectives.
That's the difference, and that's the reason D is the correct choice.