The Pittsburgh Pirates got back-to-back home runs from Bryan Reynolds and Ke’Bryan Hayes for a three-run lead in the top of the 10th inning, then held their breath to see whether their closer could hold it.

After giving up a two-run homer, David Bednar recorded three strikeouts to help the Pirates end a three-game losing streak with a 4-3 extra-inning win over the San Francisco Giants on Saturday night at Oracle Park.

Not only did the Pirates (14-14) bring their record back to .500 but they won their fourth extra-inning game to eclipse their total from last year. It also ended an 0-for-14 stretch with runners in scoring position for the Pirates in the series.

With automatic runner Alika Williams at second base to start the 10th inning, Reynolds scorched lefty Taylor Rogers’ first-pitch inside sweeper for a 387-foot homer down the left field line for a two-run home run to give the Pirates a 3-1 lead.

“It was really good because yesterday he had some opportunities he didn’t capitalize on,” Pirates manager Derek Shelton said on the SportsNet Pittsburgh postgame show. “We’re talking about a really good hitter here. If we continue to get him those opportunities, he’s going to cash in on them.”

It was a breakthrough moment for Reynolds, who went 0 for 5 Friday and struck out in his first two at-bats Saturday.

“I had a tough game (Friday), a tough start to this game,” Reynolds said, “so I just let a little bit out there.”

Hayes followed by lining an 0-1 inside sweeper 377 feet to left for his first homer, which proved to be the game-winning run.

“Taylor Rogers is probably one of the better left-handers in the game and Bryan got a breaking ball and hit it good and then Ke’ flushed it,” Shelton said. “It was good to see. Two big hits by guys we need big hits from.”

After surrendering a three-run homer to Patrick Bailey in the bottom of the ninth that lifted the Giants to a 3-0 walk-off win Friday night, Bednar gave up a two-run shot to designated hitter Jorge Soler in the bottom of the 10th. When Bednar left a 1-1 fastball over the middle, Soler sent it 433 feet to left field to score automatic runner Bailey and cut the Pirates’ lead to 4-3.

Bednar recovered to strike out Michael Conforto before Thairo Estrada singled to third. Bednar got pinch hitters LaMonte Wade Jr. and Mike Yastrzemski swinging for strikeouts for his fifth save.

Martin Perez, who held the Giants scoreless through the first five innings, allowed one run on four hits and two walks with four strikeouts for his third quality start in six outings this season.

Perez got out of a jam in the fifth, when Bailey hit a leadoff single and advanced to second when Soler drew a full-count walk. Perez got Conforto to ground into a 4-6-3 double play, then got Estrada swinging at a full-count changeup.

The Pirates failed to take advantage of extra outs after two errors by Giants first baseman Wilmer Flores in the top of the sixth. Flores dropped a Williams pop fly in foul territory, and Williams capitalized on the extended at-bat with a single. But Andrew McCutchen hit into a 6-4-3 double play. Reynolds reached when his grounder rolled under Flores’ glove but Hayes grounded out to second.

The Giants took a 1-0 lead in the bottom of the sixth, after Nick Ahmed reached on an error at short by Williams, took second on a wild pitch and scored on a double to the left-center gap by Flores.

It could have been worse. The Pirates won a challenge when Flores was called safe at third on a wild pitch but a video review showed that Hayes held the tag on Flores as he slid off the base. Jung Hoo Lee followed with a single but Perez struck out Matt Chapman.

Converted from a reliever to a starter by the Giants, Jordan Hicks recorded a career-best nine strikeouts without a walk in six innings. After Connor Joe lined a leadoff single to center to start the seventh, the Giants replaced Hicks with Luke Jackson.

When Jack Suwinski worked a full count before hitting a 108.1-mph liner off the left wrist of Ahmed, instead of a double-play ball the Pirates had runners on first and second with no outs. After Joey Bart grounded into a forceout at second, Jared Triolo drew a four-pitch walk to load the bases.

That brought to bat Rowdy Tellez, who was hitting .122 since his last RBI on April 6. When Jackson threw a first-pitch fastball over the middle, Tellez hit it to deep left for a sacrifice fly to drive in Joe for the game-tying run.

The Pirates stranded runners in scoring position in the eighth and ninth innings. After back-to-back one-out singles by Reynolds and Hayes in the eighth, Tyler Rogers got Joe to fly out and Suwinski to ground out.

In the ninth, Tellez roped a two-out single to right and pinch runner Michael A. Taylor stole second but closer Camilo Doval got Williams to ground out to first to end the rally. Hunter Stratton, who relieved Josh Fleming to get the final two outs of the ninth, earned his first major league win.

“It was fast innings, back and forth, and we were able to get some runs here and there but we weren’t able to cash in,” Hayes said. “We were able to get him out of the game and get to their bullpen and get some action going. It was great to be able to come out on top.”

Kevin Gorman is a TribLive reporter covering the Pirates. A Baldwin native and Penn State graduate, he joined the Trib in 1999 and has covered high school sports, Pitt football and basketball and was a sports columnist for 10 years. He can be reached at kgorman@triblive.com.