After taking an early lead, Juve fell behind 2-1 against their rivals, who were reduced to 10 men by Matias Vecino's 18th-minute red card, in the second half.
But two goals in the last four minutes turned the match around. Juan Cuadrado's cross deflected in off Inter defender Milan Skriniar, with Gonzalo Higuain then heading home the winner from a Paulo Dybala free-kick.
Juve now hold the advantage as they hunt a seventh straight Scudetto, although Napoli - 1-0 victors in Turin last weekend - can maintain the pressure by winning away to Fiorentina on Sunday.
Allegri, who was dismissed for encroaching on to the field in the aftermath of Higuain's winner, knows the heat will be one in the closing run of the season.
"It was a wonderful evening of football between two teams who needed to win and went all out," Allegri said to Mediaset Premium after a breathless finale at San Siro on Saturday.
"We were aggressive and played well throughout the first half, but we fell asleep in the dressing room [at half-time] and ended up behind.
"We kept putting crosses in when we should have passed it around the box to tire them out. Inter played well, but got tired in the end, crumbled physically and we took advantage.
"It's another step towards the Scudetto, even if it remains very difficult. Nothing is ever certain in football. I knew it would be tough and in the final four rounds there is always more pressure than when you play in September.
"Even a side with great mental strength will have moments where they lose confidence. Fortunately, the lads have found it again and we turned it around."
Higuain's first goal since mid-March gave Juventus a boost in the Scudetto battle against his former club Napoli, who struck a 90th-minute winner in last weekend's meeting of the top two.
"This is football," the striker told Mediaset Premium . "We lost at the last minute [against Napoli], but this time we turned it around with hunger, pride, thirst for victory.
"We will fight to the death to win. Inter played a great game, and were a man down, but it was fundamental to win and there are another three to go. It's a great confidence boost."
Juve face Bologna, Roma and Verona in their final three matches of the Serie A campaign, while Napoli take on Torino, Sampdoria and Crotone after Sunday's clash with Fiorentina, leaving little room for error from either side.