The deal is reportedly worth 3.2 million euros ($4.2 million) per season.
The 36-year-old Totti says "I'm extremely happy to play another two years with the only shirt that I love."
Totti's current deal was due to expire at the end of this season.
The deal also reportedly includes a clause that will give Totti a managerial position with his hometown club once he stops playing.
With 228 goals in Serie A, Totti is second on the all-time list behind Silvio Piola, who leads with 274.
Roma meets Lazio on Sunday.