The Phillies (80-74) moved within 1 1/2 games of first-place Atlanta after the Braves split two games in San Diego. They also trail St. Louis by five games for the second and final NL wild card with eight games left.
Philadelphia's bullpen tied a major league record with its 34th blown save, but the team still won its fourth straight and eighth in 10 games.
Brad Miller and Phillies starter Kyle Gibson also went deep, Hector Neris (4-6) pitched 1 2/3 scoreless innings and Ian Kennedy earned his 26th save and 10th since joining the Phillies at the trade deadline with a scoreless ninth.
After Pittsburgh took a 6-5 lead in the seventh on Wilmer Difo's pinch-hit drive against Cam Bedrosian, the Phillies went ahead in the bottom of the inning.
Harper, serenaded with "M-V-P! M-V-P!" chants from the 20,548 in attendance, led off with a walk. After two outs, Andrew McCutchen hit a dribbler down the third-base line that stayed fair for an infield hit.
Gregorius followed by sending on an 0-1, 80-mph slider from Chasen Shreve (3-2) over the wall in right.
"Just tried to stay on the heater and react to everything else," Gregorius said.
It was the 13th homer of the season and second in two nights for Gregorius.
"Huge, that's a huge hit," Phillies manager Joe Girardi said. "Everyone needs to contribute and that's what's going on right now."
Neris avoided blown save No. 35, but it wasn't easy. He wriggled out of a bases-loaded jam in the eighth by getting Ke'Bryan Hayes to ground out. Neris fielded the grounder, pumped his fists, jogged to first base himself and stomped on the bag with both feet before celebrating.
Girardi made a mound visit prior to Hayes' at-bat with the intention of removing Neris, but the right-hander convinced his manager to let him stay.
"Hector has been so clutch for us," Girardi said. "He's a guy that is just really tough. He is a tough kid that is durable. He's a warrior to me. I have the utmost respect for him. He said, 'I got this.' I said, 'OK, this is your last guy.'"
Said Neris, "I wanted that."
Philadelphia went ahead in the sixth with a pair of runs. McCutchen scored on a wild pitch to tie it at 4 before Ronald Torreyes made it 5-4 with a pinch-hit RBI single to center. It was the second straight night in which Torreyes had a go-ahead hit after a three-run homer in the sixth inning of Philadelphia's 12-6 comeback victory over the Pirates on Thursday night.
"Our guys grinded through at-bats and did a good job, but we came up short," Pirates manager Derek Shelton said. "It's good for our group. They need to play in games like this and feel the atmosphere like it was here tonight."
Gibson helped himself and gave the Phillies a 1-0 lead in the third with his first career homer. He entered with seven hits - all singles - in 44 career at-bats.
On the mound, Gibson allowed four runs on three hits with six strikeouts and two walks in six innings.
Miguel Yajure pitched 3 2/3 innings in his third appearance of the season and first since May 14 while dealing with right forearm soreness. The 23-year-old came on in the second inning after opener Sam Howard struck out all three batters he faced in the first and gave up three runs on five hits.
Pirates: LHP Dillon Peters (low back strain) was placed on the 10-day IL.
Phillies: CF Odubel Herrera was replaced after the fourth inning due to left ankle soreness.
Phillies LHP Ranger Suarez (6-5, 1.60) is scheduled to face Pirates RHP Wil Crowe (4-7, 5.77) in the third contest of the four-game series on Saturday.
Didi Gregorius unloads 3-run HR
Did Gregorius hits a three-run homer to put the Phillies up 8-6 in the 7th.