Philadelphia is unbeaten in regulation in its last 11 home games (9-0-2).
Brooks Laich scored for the Capitals, who had won seven of their previous eight games.
Hartnell, who has five career hat tricks, scored all of his goals in the third period. He scored on a scramble to give the Flyers a 4-0 lead at 4:45, hammered in a rebound at 7:29, and poked in a shot from the front of the left circle at 16:25.
Carter, who also had two assists, scored his goals within a span of 3:54 in the second period to move into a tie for the NHL scoring lead with Buffalo's Thomas Vanek. Both have 24 goals.
Carter put a wrist shot from the left circle off Brent Johnson's stick and between his pads at 13:11. Then at 16:05 he scored short-handed, going forehand to backhand.
It was the league-leading 13th short-handed goal for the Flyers, who matched their entire 2007-08 total.
Niittymaki, who has started the last three games in place of flu-ridden Martin Biron, was spectacular, especially in the first period when he stopped 25 shots, a new Washington record for most shots in one period on the road.
The Capitals tied their franchise record for shots in one period, set on Feb. 3, 1998 against Tampa.
It was also a Flyers' home record for shots allowed in one period. Philadelphia's record for shots allowed is 32 at Minnesota on Dec. 13, 1972.
Niittymaki, who has won six of his last seven starts, made a number of highlight-reel saves including three on Alexander Ovechkin.
Johnson, who allowed three goals on 13 shots, was replaced in goal at the start of the third period by Jose Theodore. He gave up four goals on 15 shots.
Lupul put the Flyers ahead at 4:29 of the first period, scoring his 11th goal on a wrist shot from the right circle.
Laich scored from in-close on Washington's 41st shot of the game at 5:09 of the third period.
Gagne scored his 17th goal off a pass from Carter on a power play at 9:19 of the third.
It was the first meeting between the two teams since Game 7 of last year's Eastern Conference quarterfinals when Lupul scored the game-winner in overtime.
Notes: Philadelphia C Glen Metropolit was a late scratch because of a lower body injury. He is day-to-day. ... The Flyers recalled LW Josh Gratton from the Philadelphia Phantoms, their American Hockey League affiliate. ... Washington outshot the Flyers 25-6 in the first period, and 48-28 for the game. ... Washington RW Tomas Fleischmann batted the puck out of the air and hit the post during a Capitals' power play in the first period.