Inter Milan missed a penalty, conceded an equaliser, grabbed a late winner and survived a siege on their goal before beating Empoli 2-1 on Sunday to qualify for next season's Champions League while sending their luckless opponents into Serie B.
Radja Nainggolan scored the decisive goal in the 81st minute as Inter pipped their neighbours AC Milan for the fourth and final Serie A place in next season's Champions League.
Inter finished with 69 points, behind Atalanta on head-to-head record and one point in front of fifth-placed AC Milan, whose 3-2 win at SPAL was in vain. Empoli finished with 38 points, level with Genoa, but were condemned by their head-to-head record.
"I expected a match like this," said Inter coach Luciano Spalletti, whose own future is still uncertain with media reports suggesting that the club want to replace him with former Juventus and Chelsea coach Antonio Conte.
"It was always going to be a night of suffering. We needed to put our hearts into this match and we did. I'm very unhappy to see Empoli in Serie B, I have a lot of friends there. They deserved to be saved. But we never gave up until the end."
Inter dominated the first half but were repeatedly foiled by Empoli goalkeeper Bartlomiej Dragowski whose saves included a remarkable one-handed effort to tip over Stefan De Vrij's powerful close-range header.
Inter brought on Keita Balde at halftime and the Senegal international made an impact within six minutes, firing past Dragowski with a low shot from just outside the area to put his side ahead.
Mauro Icardi then won a penalty, the referee judging that he had been tripped by Dragowski after a VAR review, but the Polish goalkeeper had the last laugh as he blocked the Argentine's poor effort with his legs.
Icardi was substituted minutes later, then the San Siro was stunned when Empoli broke down the right and Hamed Junior Traore, unmarked at the far post, tapped in after 76th minutes -- a result which would have saved Empoli and sent Inter into the Europa League.
But five minutes later, Matias Vecino powered forward, struck the post and Nainggolan turned the ball into the empty net.
Dragowski then joined the Empoli attack for a corner but Inter broke clear and Marcelo Brozovic put the ball into the empty net from the halfway line only for his effort to be disallowed after a VAR review, Dragowski having been fouled by Keita as he was running back.
The Inter player was then sent off for a second yellow card, having previously been booked for a shirtless celebration after his goal.
Handanovic had to make one more save to pull a Diego Farias cross out of the air before Inter could celebrate.