Scottie Upshall, Geoff Sanderson, Sami Kapanen and Mike Richards also scored for the Flyers, who beat the Devils for the first time in six games this season. Martin Biron, making his first start at home since the Flyers got him from Buffalo at the trade deadline, had 37 saves.
Brad Lukowich, Zach Parise, Patrik Elias and Sergei Brylin scored for the Atlantic Division-leading Devils, who lost their third straight game.
Lukowich scored the tying goal with 5:35 left in regulation after the Flyers failed to clear the puck in front.
Gagne took a pass from Randy Jones and put his own rebound past Martin Brodeur to win it.
Each time the Devils went ahead in the second, the Flyers answered quickly. Just 42 seconds after Brylin deflected in a slapshot from Elias to give the Devils a 3-2 lead, Richards tied it with a shot that went between Lukowich's legs and past Brodeur.
Upshall gave the Flyers a 4-3 lead 1:25 later with his fourth goal in nine games since he was acquired in the trade that sent Peter Forsberg to Nashville last month. Upshall had two goals in 14 games for the Predators.
Biron preserved the one-goal lead with a pad save on a point-blank shot by Scott Gomez early in the third and made an outstanding stop during a Devils' power play midway through the period.
While flat on his back, Biron turned his body, kicked his right leg up and somehow stopped a slap shot by Jamie Langenbrunner, earning a standing ovation from a home crowd that's had few reasons to cheer this season.
The Flyers, a perennial playoff participant, have the fewest points (47) in the NHL.
New Jersey is chasing Buffalo for first in the Eastern Conference.
The Devils went ahead 2-1 on Elias's 21st goal in the second period. Elias skated into traffic, stopped on a dime outside the left circle and ripped a slapshot that hit the post and went in.
Kapanen's power-play goal just 1:17 later tied it 2-2. Kapanen's slapshot was wide of the net, but hit Lukowich's shoulder and went in. The Flyers had just one goal in 32 power-play chances over eight games before Kapanen scored.
Parise gave the Devils a 1-0 lead with his team-leading 27th goal early in the first. Sanderson tied it 1-1 with a breakaway goal early in the second.
Notes: Biron is 3-6-2 against New Jersey . . . The Devils had won five straight road games . . . New Jersey is 5-0-4 against the Flyers, dating to last season . . . Philadelphia is 7-6-6 since the all-star break.