ROME (AP) — Francesco Totti's final match with Roma ended in celebration when the Giallorossi scored a 90th-minute goal for a 3-2 win over Genoa on Sunday that secured a spot in the Champions League.
Substitute Diego Perotti connected on a volley following a free kick from Radja Nainggolan for the winner.
Roma finished second, one point ahead of Napoli, which won 4-2 at Sampdoria with goals from Dries Mertens, Lorenzo Insigne, Marek Hamsik and Jose Callejon.
Napoli will enter the Champions League playoffs in August.
Juventus, which sealed a record sixth straight Serie A title last weekend, ended four points ahead of Roma.
"When you coach a squad like Roma you've got to always have victory as the goal and sometimes second place isn't acceptable for some people. But this second place is a huge accomplishment," Roma coach Luciano Spalletti said. "We had to obtain this result and today, particularly, the squad really deserves it for how the match developed."
At the Stadio Olimpico, 16-year-old Pietro Pellegri became the youngest player ever to score in Serie A when he put Genoa ahead three minutes in.
Pellegri's goal quickly quietened the Roman crowd, which had saluted Totti before kickoff.
Edin Dzeko equalized in the 10th and Roma's future captain Daniele De Rossi appeared to have the winner in the 74th but Darko Lazovic equalized for Genoa five minutes later in a wild finish.
Totti came on early in the second half.
Dzeko ended atop the league scoring chart with 29 goals, one more than Mertens.
Crotone avoided relegation with a 3-1 win over 10-man Lazio, while Empoli became the last club sent down to Serie B following a 2-1 loss at Palermo.
Andrea Nalini scored twice for Crotone, which also got a goal from Diego Falcinelli.
Crotone was completing its first season in the top division in its 107-year history. The Calabrian club was last at the season's halfway point.
Lazio defender Bastos picked up his second yellow in the first half.
Ilija Nestorovski and Bruno Henrique scored late in the second half for Palermo, which was already relegated. Rade Krunic pulled one back for Empoli three minutes from the end.
Crotone finished 17th, two points above Empoli, which entered the day one point ahead.
Last-placed Pescara, which had also already been relegated, drew 2-2 at Fiorentina.
Lazio's loss let Atalanta finish a club-record fourth, one spot ahead of the Roman club.
Also, Inter Milan routed Udinese 5-2 and Torino beat Sassuolo 5-3.
CAGLIARI 2, AC MILAN 1
Ten-man AC Milan lost at Cagliari after conceding a stoppage-time goal.
Fabio Pisacane scored three minutes into added time from a rebound following a free kick by Joao Pedro.
Joao Pedro had given Cagliari the lead 17 minutes in after being set up by Marco Borriello.
Gianluca Lapadula equalized for Milan with a penalty in the 72nd but Milan defender Gabriel Paletta was sent off with his second yellow card three minutes later.
Milan, which had already qualified for the Europa League, finished sixth.
Andrew Dampf on Twitter: www.twitter.com/asdampf