Francesco Totti bowed out with a dramatic victory as Diego Perotti's last-minute winner gave Roma a 3-2 home win over Genoa that secured a second-place finish in Serie A.
In an unbelievable ending to the game, Daniele De Rossi fired in with a powerful finish to seemingly seal victory, only for Darko Lazovic to score a header and threaten to spoil the party.
But the hosts were not to be denied, Perotti scoring in the final minute to ensure they beat Napoli to second – the third time in four years Roma have been runners-up - and ensured they will qualify automatically for the Champions League group stage.
On an emotional day, Roma legend Totti, 40, came off the substitutes' bench in his last match more than 24 years after making his debut for the club.
Fans will now wait to see if he takes up a new role with Roma or opts for a final playing stint elsewhere, while head coach Luciano Spalletti also faces an uncertain future amid big speculation linking him with the Inter job.
Pietro Pellegri, the 16-year-old who earlier this season became the joint-youngest player in Serie A history, scored his first senior goal after just three minutes as Genoa threatened to steal the show.
But Edin Dzeko's 29th goal of the season brought Roma level and ensured he will finish the season as the league's top scorer.
Roma needed a win order to secure second spot - with Napoli romping to a 4-2 win at Sampdoria - and Dzeko set up De Rossi before Lazovic's prompt response at the other end set up a frantic finish.
However, Perotti's strike ensured Roma and Totti would celebrate a dream victory on the veteran's farewell.
For him. For Roma.
The Giallorossi win 3-2 in @ Totti 's final game, and secure automatic qualification for the @ChampionsLeague group stage. pic.twitter.com/YvVWyKILIC — AS Roma English (@ASRomaEN) May 28, 2017
Genoa threatened a shock when Pellegri raced clear of Kostas Manolas to latch onto a throughball from Lazovic and slot a low, first-time finish past Wojciech Szczesny.
And it was almost two as Roma made a shaky start, Raffaele Palladino firing wide after Szczesny had taken a major risk in charging out of his goal.
But the hosts were level after 10 minutes when Kevin Strootman chipped into Dzeko, whose initial left-footed effort struck the post before he threw his body at the ball to bundle in the rebound.
Roma saw Emerson Palmieri carried off on a stretcher with what looked like a serious knee injury, but that did not halt their momentum as Dzeko forced a save from Eugenio Lamanna and Stephan El Shaarawy sent an effort narrowly wide soon after, Strootman the creator on each occasion.
Dzeko then had a penalty claim turned down just before the break when he was challenged by Genoa captain Santiago Gentiletti at the far post as he tried to turn in Strootman's cross.
Totti was brought on for Mohamed Salah in the 54th minute, as some fans applauded and others were reduced to tears.
The veteran made a positive impact, setting up Strootman to drill a long-range effort just wide before his cross led to El Shaarawy's header forcing a super save from Lamanna.
And Roma finally got in front with 16 minutes remaining, with Strootman finding Dzeko, whose impressive cut-back set up De Rossi to fire in left-footed and delight the home crowd.
Genoa levelled five minutes later against the run of play, Diego Laxalt's floating cross from the left catching out Szczesny and allowing Lazovic to net with a simple header from close range.
Szczesny then did enough to deflect a Lazovic effort onto the post in a moment that could have ended Roma's hopes of second spot as a thrilling encounter continued.
But Roma had the last word, Radja Nainggolan's cross being flicked on by both Federico Fazio and Dzeko, with Perotti lashing in from six yards against his former club.
Key Opta stats:
- Francesco Totti has made his 619th league appearance, equalling Gianluigi Buffon – only Paolo Maldini boasts more in Serie A (647).
- Francesco Totti played 786 matches with Roma in all competitions, scoring 307 goals.
- It is the first time that Roma have collected 87 points in a single Serie A season.
- For the first time in their history Roma have scored 90 goals in a single season of Serie A.
- Pietro Pellegri is the first player born in 2001 (year of Totti's league title) to score in a Serie A game.
- Pellegri is the youngest player to score a goal in this season in the top five European Leagues.