The Giants trailed 8-7 entering the eighth inning before Pillar's decisive blow in the 9-8 win Wednesday in St. Louis.
Pillar's two-run shot capped off a 4-for-5 day. The Giants center fielder totaled three RBIs in the victory.
"We just kept battling and found a way to win a ballgame against a good team."
San Francisco built an early lead with a four-run third inning. The Cardinals got an RBI single from Dexter Fowler in the third frame and a solo homer from Paul DeJong in the fourth frame to cut the Giants' lead in half. Paul Goldschmidt tied the score at 4-4 with a two-run triple in the fifth frame.
Giants shortstop Brandon Crawford answered with a three-run homer in the sixth inning, giving San Francisco a 7-4 edge. But the Cardinals answered again by scoring four runs in the bottom of the sixth frame.
Neither team scored in the seventh inning. Evan Longoria ripped a leadoff single for the Giants in the top of the eighth. Crawford grounded out in the next at-bat before reliever Giovanny Gallegos settled in against Pillar.
Pillar missed on a Gallegos slider before eyeing the 0-1 offering. He smacked the 93-mph fastball for his 21st homer of the season, giving the Giants the lead. The 440-foot moonshot had an exit velocity of 107 mph and left the field in 5.5 seconds, according to Statcast.
The Giants' bullpen closed the door on the Cardinals in the next two innings, with Will Smith picking up his 31st save of the season.
Giants starter Madison Bumgarner allowed nine hits and six runs in five innings. Cardinals starter Michael Wacha allowed two hits and two scoreless innings.
The Cardinals host the Giants in the final game of the series at 1:15 p.m. EDT Thursday in St. Louis.