Leading 2-1 in the 89th minute against an Empoli team fighting relegation, Inter's defense was in disarray as Danilo D'Ambrosio knocked an Empoli cross against his own crossbar, and soon after Inter goalkeeper Samir Handanovic was forced into a vital save.
When Empoli's goalkeeper went up for a corner and Inter counterattacked, Marcelo Brozovic scored from the halfway line into an empty net and Inter seemed secure. But more drama was to come as the video assistant referee ruled the goal out because Empoli's keeper was fouled as he hurried back to his goal and had Keita Balde — the scorer of Inter's first goal — sent off.
Empoli charged forward again, forcing Hamdanovic to save a header in the eighth minute of injury time but Inter held on for a 2-1 win — with the second goal from Radja Nainggolan — to secure Champions League football for a second straight year.
Four teams had been fighting for the last two Italian Champions League spots on Sunday. All won their games as Atalanta took third and Inter fourth, but none had it easy. Atalanta went a goal down to Sassuolo early on but fought back to win 3-1 after Sassuolo had Domenico Berardi sent off for his part in a brawl.
AC Milan fluttered in and out of fourth position throughout the evening thanks to goals elsewhere and its struggles with SPAL in a 3-2 win sealed by two goals from Franck Kessie. However, Inter's tenacity and Atalanta's comeback meant that Milan had to settle for fifth — a point behind Inter and Atalanta — and the Europa League.
Roma finished in sixth. It had started the day an outside contender and stayed that way despite an 89th-minute goal from Diego Perotti securing a 2-1 win over Parma. That marked the 616th and last appearance in a Roma shirt for midfielder Daniele de Rossi, who leaves the club after 18 seasons.
Next season Roma will be out of the Champions League for the first time since 2013-14, while Milan's wait to return to the Champions League enters a sixth season. Further back, Torino beat Lazio 3-1 to secure seventh.
ALLEGRI SIGNS OFF Massimiliano Allegri's reign as Juventus coach finished with a 2-0 loss to Sampdoria, an uncharacteristically flat ending for a coach who oversaw near-total domination of Serie A. That was the fourth game in a row without a win for Juventus, which has seen a slump in form since it sealed its eighth consecutive Italian title last month.
Sampdoria held Juventus at bay before Gregoire Defrel scored in the 84th minute off a neat one-two with Manolo Gabbiadini. Gianluca Caprari curled a free-kick into the net in added time. Allegri is leaving at the end of the season, a year before his contract ends, and no successor has been appointed.
Juventus finished with 90 points and an 11-point lead over second-place Napoli. EMPOLI RELEGATED Empoli's desperate fight against Inter wasn't enough to stay in Serie A, as Genoa stayed up on its superior head-to-head record after earning a valuable point in a 0-0 draw with Fiorentina.
Empoli joined Frosinone and Chievo, which were both already relegated. Brescia and Lecce have secured promotion from Serie B for next season, with the third promoted team to be decided in playoffs.
More AP soccer: https://apnews.com/apf-Soccer and https://twitter.com/AP_Sports