TORONTO — Auston Matthews scored the tiebreaking goal with 6:06 left in the third period and the Toronto Maple Leafs rallied from an early two-goal deficit to beat the Tampa Bay Lightning 4-3 on Tuesday night and take a 3-2 lead in the first-round playoff series.

William Nylander added a goal and two assists for Toronto, which is a victory away from advancing in the postseason for the first time since 2004. John Tavares had a goal and assist, and Morgan Rielly also scored. Jack Campbell stopped 32 shots.

Steven Stamkos, Victor Hedman and Ryan McDonagh scored, Nikita Kucherov had two assists and Andrei Vasilevskiy made 22 saves for the two-time defending Stanley Cup champion Lightning, who will try to avoid elimination in the best-of-seven series in Game 6 at home on Thursday night.

Game 7, if necessary, would be in Toronto on Saturday.

Matthews snapped a 3-3 tie on a 2-on-1 rush in the third period. Mitch Marner fired a shot off Vasilevskiy’s right pad and Matthews, ,who had 60 goals during the regular season, scored his third of the playoffs to ignite the raucous home crowd.

The Lightning pulled Vasilevskiy late and had a couple of chances, but iced the puck with 21.9 seconds left to force the 2021 Conn Smythe Trophy winner back into his crease.

Down 2-1 heading to the third, Toronto tied the score with the teams playing 4 on 4 at 3:01 when Tavares held onto the puck in the offensive zone before finding Rielly, who scored his first of the series.

The Leafs took their first lead when Nylander scored his third goal in the last two games on Vasilevskiy.

But the Lightning responded at 8:17, when McDonagh blasted a shot past Campbell, tying the game at 3 before Matthews won it.

The Maple Leafs were coming off a 7-3 loss to Tampa Bay in Game 4.

Toronto head coach Sheldon Keefe went with the same lineup for the third straight game. He shook up three of his team’s four lines. That included splitting up Tavares and Nylander, while Michael Bunting returned to the top trio with Matthews and Marner.

Toronto got an early power play to continue the series’ parade to the penalty box, but couldn’t connect despite some good looks for Marner.

Stamkos made it 1-0 at 5:19 of the first. The Lightning then got their first man advantage when T.J. Brodie went off for hooking, and they went up by two at 6:11 when Hedman fired a shot that found its way through traffic.

Toronto killed off a 5-on-3 power play for 30 seconds later in the period thanks to some big stops from Campbell before Nylander broke in alone coming out of the box, only to be denied by Vasilevskiy.

Tampa Bay finished the period that included seven minor penalties with a 14-5 edge in shots.

The Lightning were whistled for their second infraction of the night for too many men on the ice early in the second, and Toronto broke through when Nylander fired a shot that went in off the skate of Tavares for the captain’s first goal of the series.

The Leafs continued to push as the period wore on, with Vasilevskiy forced to make a number of huge saves.

Campbell had a much quieter period at the other end, but had to make a big stop on a Nick Paul breakaway after he stripped Morgan Rielly of the puck.