Inter Milan came from behind to snatch the final Champions League place in their 3-2 winner-takes-all victory at Lazio.
The home side, who only needed to draw, were leading 2-1 at half-time after Adam Marusic and Felipe Anderson had scored either side of a Danilo D’Ambrosio effort.
But the game changed when Stefan de Vrij — who has reportedly agreed a move to Inter for next season — gave away a penalty from which Mauro Icardi levelled.
Senad Lulic was sent off for a second booking shortly afterwards and Matias Vecino headed home from a corner with less than 10 minutes remaining to seal a remarkable win.
There had been drama shortly before Icardi’s equaliser when Inter were awarded a penalty for a push by Marusic, only for the Video Assistant Referee to overturn the decision.
Victory moved Inter level on points with Lazio, who had a significantly better goal difference, but head-to-head records are used in Serie A meaning Luciano Spalletti’s men finished fourth.
Lazio’s fierce rivals Roma ended their campaign with a narrow 1-0 win at Sassuolo.
The only goal of the game came in the closing seconds of the first half when defender Kostas Manolas’ attempted cross somehow crept in.
SPAL and Cagliari saved themselves from the possibility of relegation with final-day victories, meaning a return to Serie B for Crotone.
Crotone had to beat Napoli to have any chance of survival and then hope other results went their way, but neither permutation materialised after a 2-1 loss.
Goals from Arkadiusz Milik and Jose Callejon put second-placed Napoli in control before Marco Tumminello’s late reply for the Calabrian side.
Two goals from Mirco Antenucci and another by Alberto Grassi saw SPAL take command against Sampdoria, helped by a red card for visiting forward Gianluca Caprari. Dawid Kownacki pulled one back for Sampdoria but SPAL were 3-1 winners.
Luca Ceppitelli struck three minutes from time as Cagliari defeated Atalanta, who still finished seventh and qualified for the Europa League as Fiorentina were thrashed 5-1 by AC Milan at the San Siro.
Milan wrapped up a Europa League group stage place at the end of Gennaro Gattuso’s first season in charge. Hakan Calhanoglu quickly equalised after Giovanni Simeone’s 20th-minute opener before further goals from Patrick Cutrone (2), Nikola Kalinic and Giacomo Bonaventura.
Elsewhere, Seko Fofana’s strike gave Udinese a 1-0 home win against Bologna and Chievo beat bottom club Benevento by the same scoreline courtesy of Roberto Inglese’s goal.
Torino won 2-1 at Genoa thanks to goals from Iago Falque and Daniele Baselli, although they finished the match a man down when Cristian Ansaldi was sent off shortly after Goran Pandev’s 80th-minute reply.