SAN FRANCISCO — Pablo Sandoval was still sitting in the home dugout at AT&T Park, his day long done, when the Giants completed their biggest comeback of the season.

The 30-year-old third baseman celebrated his return to San Francisco with a big swing in the seventh inning that jump-started the Giants’ rally then stuck around long enough to see it finished.

Denard Span scored on Jarrett Parker’s two-out infield single in the 10th inning, lifting San Francisco to a 5-4 victory over the Arizona Diamondbacks on Saturday night.

“I was excited to be back playing third, hitting cleanup,” a grinning Sandoval said. “We pulled it out. My teammates did a great job fighting to the last inning.”

It was Sandoval’s first game with the Giants since 2014 when the franchise won its third World Series in five years. He had spent the past two weeks in the minors after signing with San Francisco on July 22 after he was released by Boston, which signed him to a $95 million, five-year deal following the 2014 season.

The Giants trailed 4-0 and had only three hits when Sandoval opened the seventh with a double. He scored on Parker’s RBI double, and Hunter Pence followed with a two-run home run.

It was a much-needed lift for the slumping Giants and for Sandoval after he was called up from the minors earlier in the day. The 2012 World Series MVP, who was greeted with a standing ovation and a smattering of boos from the AT&T Park crowd before his first at-bat, had committed a costly throwing error that led to an unearned run in the fifth.

“That kind of woke us up because we looked dead in the water,” San Francisco manager Bruce Bochy said. “Nice piece of hitting by Pablo going the other way. At that point, now it’s a different feel in the dugout.”

Span led off the 10th with a double off T.J. McFarland (4-4) past Arizona right fielder J.D. Martinez. After Kelby Tomlinson was intentionally walked and Brandon Crawford hit into a double play, Buster Posey was intentionally walked before Parker beat out a grounder to second baseman Adam Rosales as Span crossed the plate.

It was Parker’s first game-ending hit and marked the first time that the Giants have won after trailing by four or more runs since Aug. 18, 2016.

“Honestly, I made the pitch I wanted to make there at the end and it just found a hole,” McFarland said. “It’s just one of those things where it found the perfect spot. I threw a sinker and (Parker) chopped it 5 feet in front of the plate but it just kept rolling up the middle and found that hole.”

David Peralta had three hits and an RBI, Martinez doubled and tripled and Jeff Mathis extended his hitting streak to 11 games for the Diamondbacks.

The Giants trailed 4-0 but scored three times in the seventh capped by Pence’s two-run homer, then tied it in the eighth when Tomlinson came home on Posey’s double-play grounder.

Arizona loaded the bases in the ninth and had the potential go-ahead run at third in the 10th but failed to score.

Hunter Strickland (2-2) retired three batters for the win.

20-YEAR REUNION

The Giants honored their 1997 team during a pre-game ceremony. Several players from that team were on hand, including home run king Barry Bonds. San Francisco won the NL West that season, one year after finishing last in the division.

TRAINER’S ROOM

Giants: 1B Brandon Belt was placed on the seven-day concussion list after getting hit in the head by a 78.9 mph curveball from Arizona rookie Anthony Banda on Friday night.

UP NEXT

Arizona LHP Patrick Corbin (8-9, 4.36 ERA) seeks his fifth career win against San Francisco in the series finale Sunday afternoon while the Giants will go with RHP Jeff Samardzija (6-11, 4.77 ERA). Samardzija has won his last two decisions against the Diamondbacks.