The Belgium international, who scored 11 league goals last season as Roma finished second behind champions Juventus, had also been a reported target for Inter.
"I think I have demonstrated that this is what I always wanted," Nainggolan said.
"So I'm very happy, especially that the club and I can continue our adventure and move forward together."
Nainggolan signed for Roma from Cagliari in January 2014 and the 29-year-old has so far made 161 appearances for the club, scoring 27 goals.
President James Pallotta hailed Roma's ability to hold on to the in-demand midfielder, having previously been adamant that the player was going nowhere.
He said: "We're delighted Radja has committed his long-term future - and the most important years of his career - to the club.
"Last season, he was arguably one of the best midfielders in the world and it's no surprise that he was linked with some of the biggest clubs in Europe.
"But there was never any question of him going elsewhere - he loves Roma and we love having him here."