The Old Lady had been heading for a 2-0 defeat until the 85th minute, when a quickfire brace from new number 10 Paulo Dybala brought things back level - only for youngster Alessandro Murgia to find the winner with virtually the last kick of the game.
Juve made a bright start to the encounter and could have been one goal up with less than three minutes on the clock, but Juan Cuadrado's shot from close range was kept out by a smart save from Thomas Strakosha.
As the half wore on, Lazio started to come into the game and almost broke parity on the half hour mark when Sergej Milinkovic-Savic's shot went just over the bar.
It was not too long before they claimed an unlikely lead, however, after being awarded a penalty when Ciro Immobile was taken out by Juve keeper Gianluigi Buffon when through on goal. The former Juve youth star made no mistake with the resultant spot kick, planting it comfortably down the right to evade Buffon.
Juve came out of the traps flying in the second half, Miralem Pjanic and Dybala having the best of the chances, but the ball was in the back of the net at the other end of the pitch as the hour mark approached.
Immobile could have wrapped up his hat-trick inside an hour but Buffon was quick off his line to thwart his attempt from close range as Juve looked increasingly bereft of ideas.
Stefan de Vrij was lucky not to see red when he fouled Dybala on the edge of the area, Pjanic sending the resulting free kick narrowly over the crossbar, as Allegri brought on Douglas Costa for Cuadrado.
With just five minutes of regular time remaining, Juve were firmly back in the contest thanks to a moment of brilliance from Dybala. Taking a free kick from 27 metres out, the Argentine curled the ball expertly around the wall and in past the left post, leaving Strakosha with no chance.
Three minutes later it looked as though a remarkable comeback could be on the cards, Dybala this time scoring from the spot after Adam Marusic felled Alex Sandro inside the box, and it was momentum Juve in stoppage time as Allegri's men looked to avoid extra time.
There was to be one more twist in the tale though, as 21-year-old academy graduate Murgia secured the silverware for his side with mere seconds remaining. Jordan Lukaku - brother of Manchester United striker Romelu - was the provider, sending his pass from the side of the box to an unmarked Murgia, who completed the famous win with a simple tap-in.
Juventus begin their league title defence next Saturday at home to Cagliari, while Lazio are at home to newly-promoted SPAL on Sunday.