Lazio stunned Rudi Garcia's side when they took a 2-0 half-time lead following goals from Stefano Mauri and Felipe Anderson.
Totti reduced arrears with a fine angled strike three minutes after the restart and claimed his second just after the hour to set up a tense finale.
It was Totti's 239th goal in Serie A, taking the 38-year-old to within 35 of Silvio Piola's all-time record of 274.
Totti, who is in his 22nd season with the club, celebrated by taking a telephone from one of the Roma coaching staff and taking a 'selfie' picture of himself before returning to the pitch.
Totti's 11th goal in the city derby was also a record, and he told Sky Sport: "I had the idea during the week. After scoring my 11th goal it was an unforgettable moment."