There's no works that they did. We don't have to do anything in the way of works to get on the vine. The fruit has to be produced once we are on the vine. So there's no contradiction there. Let's just go back. Notice the tent. If you look at the tents, it usually makes it clear. He says, "By grace what? You have been saved." What tense is that? That's past or past perfect if you want to be technical. Okay. So yeah, he's talking about, yeah, when you got saved, that was purely by grace. There were no works involved.
Now, some people come to baptism with having done a lot of works. That's admirable. But a person can be saved coming to baptism without any at all. But here in Romans 2, he's speaking about the second phase of salvation where works do matter. So Paul doesn't contradict himself. There's a verbal contradiction, but it's easily harmonized.