The answer is quite simple. There were a group of Jews in Paul's time who believed that they could be saved by keeping the Law. This is abundantly clear from Romans 2:17, “Behold, thou art called a Jew, and restest in the law, and makest thy boast of God.” Those Jews were RESTING, or relying upon the keeping of God's Law, to save them. The Apostle Paul goes on, from Romans 2:18 to Romans 3:19, to inform the Jews that they hadn't done a very good job of keeping the Law and were a bunch of hypocrites! He concludes his indictment against them with Romans 3:20, “THEREFORE by the deeds of the law there shall no flesh be justified in his sight: for by the law is the knowledge of sin.”
All true but, again, it's important to ask what Paul meant by "deeds of the law". More to the point, it is important to not assume that "deeds of the law" is the same concept as "good works".
I suggest it is not and that we are therefore not forced to deny the teaching of final salvation based on good works that Paul so clearly espouses in Romans 2:6-7. Paul generally uses the phrase "works" (or deeds) to denote the practices of the
Law of Moses which, despite misunderstanding about this, was given
exclusively to Israel. More specifically, when Paul asserts that one is not saved by "works of the Law" (or, more concisely "by works), he is really simply saying
salvation is not limited to Jews. No doubt there will be howls of objection - if anyone is left that is actually willing to stand up for their belief that good works are not necessary for salvation - that I am bending the concept "works" out of shape in asserting that it is really a coded way to refer to
ethnicity. Well, fair enough. But I am prepared to vigorously defend that interpretation with plenty of Biblical evidence (although if the moderators will permit, not in the present post).
But for starters, I will draw an analogy. Imagine that there is a religious group called the Sneetches that have a code - the law of the Sneetches" - that only they (Sneetches) followed and that governed all aspects of their lives - how they dress, what holidays they take, what food they eat, what moral rules they follow. Suppose further that there are elements of this code that are designed to clearly tell the rest of the world "we are Sneetches and we are an insular, closed group different from the rest of you". So, for example, their code might prescribe the wearing of bright blue hats to clearly distinguish themselves from the rest of society. Now suppose further, these Sneetches believed that only Sneetches go to heaven. Along comes a Sneetch who has a revelation that "god" wants everyone in heaven, not just Sneetches. So that person, "Ed the Sneetch", writes "you are not saved by doing works of the law of the Sneetches".
In this context, this is clearly a coded way of saying "Not only Sneetches can be saved". It is certainly
not a denial that people, Sneetch or non-Sneetch, need to do good deeds to be saved, even though the law of the Sneetches does indeed have
elements that prescribe good deeds.
The thing you need to remember is that while the Law of Moses does prescribe "good works", a huge chunk of it has
nothing really to do with good works, but instead functions to mark out the Jew as distinct from the Gentile.
Paul's concern is that (many) Jews thought salvation was a privilege reserved for Israel
alone. So he has argues against this saying "you are not saved by works of the Law". I will bet he is stroking his beard in "heaven" now, wishing he had used different phrasing since, seemingly, people have not understood him and, in the process, shockingly decided to discarded this Pauline teaching:
6God “will repay each person according to what they have done.”7To those who by persistence in doing good seek glory, honor and immortality, he will give eternal life. [Romans 2:6-7, NIV]