Three points separated the two teams either side of the dotted line before kickoff, with fourth-placed Lazio needing to simply avoid defeat to guarantee their place in the competition next season.
The hosts looked to be on course for the win when they went into half time with a 2-1 lead courtesy of an Ivan Perisic own goal and Felipe Anderson strike either side of Danilo D'Ambrosio's equaliser, but Inter launched a dramatic late comeback to steal fourth place.
Mauro Icardi levelled things up in the 78th minute before Senad Lulic was shown a red card just one minute later, and an action-packed spell then saw Matias Vecino score the winner nine minutes from time.
The result means that Inter leapfrog Lazio into fourth place by virtue of their superior head-to-head record, consigning the capital outfit to Europa League football next season as Inter qualify for the Champions League for the first time since 2011-12.
The visitors may not have predicted that the night would end in such euphoria when they fell behind after only nine minutes, with Perisic inadvertently turning the ball into his own net to give Lazio a perfect start.
It almost got even better for the hosts midway through the first half when Sergej Milinkovic-Savic - a rumoured target of Manchester United this summer - met a free kick only to see his effort come back off the woodwork with the keeper beaten.
Inter levelled things up shortly before the half-hour mark when the ball eventually fell to D'Ambrosio after a scramble inside the area, and he prodded his finish past Thomas Strakosha from close range.
A draw would still have sufficed for Lazio in their bid to finish in the top four, but they were taking no chances and put themselves in an even more comfortable position again four minutes before half time when Felipe picked out the bottom corner after bringing the ball under his spell on the edge of the box.
Inter's hopes may have begun to fade away as the second half progressed with clear-cut chances difficult to come by, but they were suddenly given a lifeline in the 78th minute when Icardi was brought down inside the area.
The Inter skipper stepped up to take the spot kick himself and made no mistake from 12 yards to restore parity on the night, kickstarting a manic three-minute spell which would ultimately define the seasons of both clubs.
Lazio still had Champions League qualification in their hands with the score at 2-2, but just one minute after the equaliser they saw their chances of holding on take a significant blow when Lulic picked up a second yellow card, leaving the home side with 10 men for the final 11 minutes.
It did not take long for Inter to make the most of their numerical advantage, scoring their dramatic winner just three minutes after levelling the scores when Vecino rose highest from a corner to glance his header past the keeper and send the away fans wild.
For the first time Lazio found themselves needing a goal, but they could not find one in the final nine minutes and even picked up a second red card late on, with Patric being given his marching orders despite not being on the pitch.
The final whistle was greeted by scenes of jubilation and despair in equal measure as Inter celebrated one of the most memorable finales to a Serie A season ever, while Lazio are forced to settle for a fifth-place finish and Europa League football next term.
LAZIO (3-5-1-1): Strakosha; Felipe, De Vrij (Nani 84'), Radu (Bastos 77'); Marusic, Murgia, Lucas, Milinkovic-Savic, Lulic; Felipe; Immobile (Lukaku 75')
INTER MILAN (4-2-3-1): Handanovic; Cancelo, Skriniar, Miranda, D'Ambrosio; Vecino, Brozovic, Candreva (Martins 61'), Rafinha (Karamoh 68'), Perisic; Icardi