The Leafs centre snapped a 5-5 tie with his 33rd goal of the season on a quick shot through the pads of Flyers netminder Brian Elliott with exactly five minutes to go after taking a one-handed, desperation feed from a falling William Nylander inside a raucous Scotiabank Arena.

Matthews added his 34th — matching his jersey number — with 2:39 remaining on a rebound off the rush after Andreas Johnsson took the initial shot.

Former Leafs winger James van Riemsdyk completed the fourth hat trick of his career on a tip with 1:14 left to get Philadelphia back within one, but Toronto held on as Andersen recovered from some questionable moments to rob Shayne Gostisbehere in the dying seconds.

"The guys did a hell of a job," said Andersen, who was pulled in consecutive losses for the first time in his career earlier this week. "It seems like everything's going against me.

"You've got to find a way to fight through it."

Jake Muzzin, with two goals and an assist, Patrick Marleau, Zach Hyman and Martin Marincin also scored for Toronto (43-23-5), which snapped a two-game losing streak and crept within two points of Boston for second in the Atlantic Division.

"We'd like to do it in a different manner," Muzzin said. "But a win's a win."

Andersen finished with 23 saves for the Leafs, who were wearing their throwback green and white Toronto St. Patricks jerseys. Nylander, Nazem Kadri, Nikita Zaitsev and Ron Hainsey each had two assists.

The Leafs, who held a dominant 96-49 advantage in shot attempts, visit the last-place Ottawa Senators on Saturday in the second game of a back-to-back.

Gostisbehere and Radko Gudas, with a goal and an assist each, and Jakub Voracek provided the rest of the offence for Philadelphia (34-29-8). Claude Giroux had three assists, while Nolan Patrick and Sean Couturier added two apiece. Elliott made 44 saves as the Flyers dropped a crucial two points to damage their fading playoff hopes.

"We know what kind of team they are," van Riemsdyk said. "They can score at will. They're never out of a game."

Coming off consecutive brutal home-ice performances — Monday's 6-2 loss to Tampa Bay and Wednesday's 5-4 loss to Chicago, where they trailed 5-0 late in the second period before a late rally — the Leafs fell behind 5-2 before two goals from Muzzin set up Marincin's equalizer early in the third.

The defenceman, who hadn't scored since Nov. 11, 2016, against Philadelphia, fired a shot from the point that went off the glove of Flyers blue-liner Robert Hagg and in at 2:55.

Andersen nearly bobbled the puck into his own net later in the period, but managed to recover before disaster struck to help set up the wild finish.

"I'd love to handle it better and get out of it quicker," Andersen, who after Philadelphia's fifth of the night had given up 13 goals on 52 shots in his last three games, said of how he's handling this rough stretch.

"Just trying to work my way out of it."

Tied 1-1 after the first, the Flyers went ahead at 1:15 of the second when van Riemsdyk tipped in his 22nd. Van Riemsdyk, who spent six seasons in Toronto before returning to the team that drafted him this summer in free agency, made it 3-1 on a chance in tight that dribbled through Andersen at 8:26.

Back in the lineup after a one-game absence because of the flu, Hyman scored his 17th on a slick setup by Mitch Marner just 61 seconds later, but Gudas restored the Flyers' two-goal lead with his third, and first in 37 games, on a shot that Andersen will likely want back.

Gostisbehere made it 5-2 less than a minute later when he swept home his seventh after a scramble in front.

Muzzin scored his seventh from a tough angle on Elliott at 16:08 to breathe life into the Leafs before the big defenceman added his second off a harmless-looking rush on a shot that fooled the Philadelphia goalie at 18:11 to get Toronto within one.

The Flyers, who lost 5-2 at home to Washington on Thursday to snap a 6-1-1 streak, dressed 11 forwards and seven defenceman with winger Ryan Hartman out with an illness. Toronto was still minus forward Kasperi Kapanen (concussion) and defencemen Jake Gardiner (back) and Travis Dermott (shoulder).

After back-to-back games where they struggled early, the Leafs got off to a good start, but the Flyers got on the board first when Voracek, who was coming off a two-game suspension, ripped a one-timer for his 19th at 12:22.

Toronto tied it at with 2:15 left in the first on the power play when Nylander's shot went off Marleau for his 15th.

The Leafs appeared to go ahead with 56.8 seconds remaining when Matthews batted home his own rebound, but the goal was waved off after video review showed the net was slightly dislodged.

The decision was met with loud boos and was eerily similar to the dying seconds of Wednesday when Chicago netminder Collin Delia nudged the same net off its moorings as Toronto desperately pushed to tie.

Andersen stopped Giroux on a breakaway in the closing moments of the first to keep things even heading to the intermission.

"The guys did a heck of a job for Freddie," Leafs head coach Mike Babcock said. "Freddie's done that for us a bunch.

"That's just payback."

NOTES: The Leafs were known as the St. Patricks from 1919 until 1927 when the team was bought by Conn Smythe. ... Toronto will also wear green and white Saturday in the nation's capital. ... Philadelphia visits Pittsburgh on Sunday. ... Voracek has 12 points in his last six games (three goals, nine assists)