Juan Manuel Iturbe put the nominal visitors ahead at the Stadio Olimpico, but Filip Djordjevic levelled for the hosts, who needed a win to move above their rivals.
A header from Mapou Yanga-Mbiwa two minutes later decided the derby in Roma’s favour and put them into the Champions League, leaving Lazio with a thrilling final to come next weekend when they head to Napoli.
They need to avoid defeat there to secure third, but losing would see Napoli move above them on the final day of the season and relegate Lazio to the Europa League.
With Lazio needing a win to move ahead of Roma and lead the race for a place in the Champions League going into the final weekend, it was inevitable that they showed more desire to get the points.
Roma, meanwhile, needed just a point to remain ahead on that race and a win to secure second with a game to spare and their initial focus was on defending.
Antonio Candreva created the first moment of danger when he got round the back of the Roma defence and sent in a low cross which was blocked and nearly fell for Miroslav Klose just in front of goal.
Another cross from Candreva was headed wide of the far post from just five yards out by the German striker, who was as stunned as most of the Stadio Olimpico that the ball had not hit the back of the net.
Roma changed their approach in the second half with a draw no longer seeming to be their aim, and the game opened up.
Victor Ibarbo sent a shot wide which seemed as easy as Klose’s chance before Alessandro Florenzi had an effort deflected wide.
It was Roma’s turn to dominate the derby and they took the lead in the 73rd minute with Iturbe finishing off an excellent team move.
Radja Nainggolan slipped the ball through to Ibarbo, who did not bring the ball under control, but that actually favoured him as it made room for a low cross which Iturbe met six yards out to poke in.
Lazio were forced to respond to chase their Champions League dream and it was not long before they levelled.
Felipe Anderson’s cross was headed back across goal intelligently by Klose and Djordjevic just had to make contact with his head to bring the hosts level.
Roma this time did not sit back and settle for the draw the way they had done at the start of the game and a free-kick saw them regain the lead with just five minutes remaining.
Yanga-Mbiwa’s flicked header guided the ball into the far corner of Federico Marchetti’s goal to earn the Giallorossi their win and place in the Champions League next season.