PITTSBURGH -- Starling Marte made a baserunning blunder in the seventh but more than made up for it two innings later.
Marte hit the first pitch from reliever George Kontos (2-2) into the left-field bleachers for his 15th home run of the season and second walk-off homer of his career.
"To walk with a home run is fun," Marte said.
The left fielder had two of the Pirates' four hits and extended his hitting streak to seven games, but had to walk off the field in embarrassment in the seventh.
With the scored tied, 1-1, in the seventh inning, Marte hit a leadoff double but was easily caught trying to steal third base when he took off while rookie reliever Hunter Strickland was standing on the mound holding the ball.
Rookie shortstop Jung Ho Kang followed with his second home of the game, putting the Pirates ahead 2-1 instead of 3-1.
"He's an exciting player and he sometimes tries to get some things done and make some things happen, and he thought he'd seen a routine where the pitcher had fallen into a pattern, and it didn't work," Pirates manager Clint Hurdle said.
"It never looks good when you're out in that situation. He understands it."
Kontos retired the first five batters he faced, striking out three, after relieving Strickland to start the eighth inning before Marte took him deep.
"Today was the best I've possibly felt all year," Kontos said. "I felt I was doing a good job of delivering my pitches, hitting my spots, but one pitch I didn't execute as well I'd like and I think he was sitting on it, and took advantage of it."
The Giants tied the score, 2-2, in the eighth when third baseman Matt Duffy scored on a two-out wild pitch by right-handed reliever Joakim Soria, who seemed on his way to getting out of the inning by inducing left fielder Nori Aoki to hit into a double play.
Pittsburgh (73-48) won the fourth time in its last five games and sent San Francisco (66-57) to its fifth loss in seven games.
"It's a tough one to lose because we played a good ballgame," Giants manager Bruce Bochy said. "We ran the bases well, made some things happen, but it turned out just not to be quite enough."
Kang hit long home runs to center field in consecutive at-bats with the bases empty for his first multi-homer game in the major leagues. He broke up right-hander Mike Leake's no-hit bid in the fifth to tie the score before connecting off Strickland.
Kang has 12 home runs this season. He starred in the Korean Baseball Organization before signing with the Pirates in January as a free agent.
"I'm going to do my best to take it to another level," Kang said through a translator. "Just working hard to get to another level."
Pirates right-hander Gerrit Cole remained stuck on 14 wins since July 26 despite limiting the Giants to one unearned run and three hits in seven innings, with eight strikeouts and three walks.
Cole entered the month leading the major leagues in victories, but has gone 0-3 with a 3.60 ERA in five August starts.
Leake was outstanding in his second start with the Giants.
Activated from the disabled list before the game after being out since Aug. 3 because of a strained left hamstring, Leake allowed only one run and one hit in six innings, with six strikeouts and one walk.
Leake retired his first 11 batters before walking center fielder Andrew McCutchen with two out in the fourth.
Leake is 0-1 with the Giants despite a 2.13 ERA since being acquired from the Cincinnati Reds on July 30 in a trade. In his last six starts, including his final four with the Reds, Leake is 4-1 with a 1.05 ERA.
The Giants broke a scoreless tie in the top of the fifth on second baseman Ahire Adrianza's RBI single.
NOTES: Giants OF Ryan Lollis was optioned to Triple-A Sacramento to open a roster spot for RHP Mike Leake to be activated from the 15-day disabled list. ... Giants CF Angel Pagan is ready to begin a rehab assignment, according to manager Bruce Bochy, after being on the disabled list since Aug. 10 because of patellar tendinitis in his right knee. ... The Giants still aren't sure when RHP Tim Hudson, on the disabled list since July 27 because of a strained right shoulder, will be able to make a second rehab start after last pitching Aug. 14 for high Class A San Jose. ... The series concludes Sunday night with San Francisco RHP Ryan Vogelsong (9-8, 3.93 ERA) facing Pittsburgh LHP
Francisco Liriano (8-6, 3.35 ERA).