Both teams were on level terms when the Roma veteran fired home five minutes from the final whistle to spoil his ex-Italy compatriot's farewell.
Milan legend Maldini was set to hang up his boots last season, but he put off his retirement till the end of the current season after signing a one-year extension.
The 40-year old defender made his debut with the Rossoneri on January 20th 1985, and is considered one of the cult figures in Italy.
He played over 1000 games throughout his glittering career, having won five Champions League and European Cups, four European Super Cups, seven Serie A titles and five Super Cups.
Maldini, who will celebrate his 41st birthday in June, has also impressed on the international stage and won 126 Italy caps from 1988 till 2002.
Hard-fought win
Roma's John Arne Riise broke the deadlock on the stroke of half-time, and Massimo Ambrosini leveled for Milan on the 75 minutes.
Ten minutes from time, Jeremy Menez restored Roma's slender lead, but Ambrosini equalized again seconds later.
Totti sealed the hard-fought win late on 85 minutes.
Ambrosini received his marching orders two minutes from time after having an altercation with referee Andrea De Marco.
Milan are now in second place on 71 minutes, several goals ahead of third-placed Juventus, while Roma are sixth with 60 points.