SAN FRANCISCO — One moment, Mac Williamson hit the go-ahead homer for his first career long ball as tens of thousands cheered him. The next, he misjudged the wind in left field and lost a fly ball for a two-base error that could have cost San Francisco the game.

Only when the bullpen pulled it out could Williamson exhale.

He homered leading off the eighth inning and later got the ball back with a trade of a signed bat and ball, and the Giants held on to beat David Price and the Boston Red Sox 2-1 on Wednesday night to split the quick two-game series.

“The game’s humbling, and it couldn’t have happened any quicker than it did to me tonight,” said Williamson, who was anticipating a cut fastball inside after Price had already struck him out twice with it.

Brandon Belt also connected for the Giants, who finally got to Price (7-3). He struck out seven over eight innings, allowing three hits and two runs with two walks.

The Giants snapped a three-game losing streak.

“To be able to help the team win, not just get my first home run, but have it be a meaningful home run and help the team win, especially at this point in the season, is really special,” Williamson said.

Price and Madison Bumgarner faced off in a rare matchup between two of baseball’s top left-handers representing each league, to which Boston manager John Farrell said, “It was as billed.” Both pitchers surrendered only a home run before Williamson sent the first pitch deep into the bleachers in left field.

“He’s facing one of the elite pitchers in the game,” manager Bruce Bochy said. “He got enough of it. He’s strong.”

Cory Gearrin (2-0) struck out two in a 1-2-3 eighth for the win. Santiago Casilla entered and Williamson immediately committed a two-base error when he dropped Hanley Ramirez’s leadoff fly in the ninth, the ball glancing off the outside edge of his glove. One out later, Javier Lopez relieved to face pinch-hitter David Ortiz, who walked.

Lopez struck out Travis Shaw then Hunter Strickland retired pinch-hitter Marco Hernandez on a groundout for his second career save and first since 2014.

Belt splashed an 0-1 pitch into McCovey Cove for the 69th by a Giants player and first since he did so in September 2014. This was his fifth splash homer, while home run king Barry Bonds hit 35 of them.

This marked the first career start at AT&T Park for Price, who received a $217 million, seven-year contract to join Boston during the offseason.

“It was one pitch. I’m not going to let it snowball,” he said. “… It’s a loss. You have to take the good with the bad.”

Chris Young homered in the fourth for Boston and Dustin Pedroia extended his majors-best hitting streak to 15 games with a leadoff single in the sixth.

After San Francisco’s five-game home winning streak was snapped in a 5-3, 10-inning loss Tuesday night, the Giants earned just their fifth victory in the last 14 meetings with Boston and only the third in 11 at AT&T Park.

The Giants have won each of the last nine starts by Bumgarner — who wasn’t happy with his six-inning outing — and the 2014 World Series MVP’s stretch of six straight winning decisions held since an April 20 loss to Arizona.

Williamson became the first Giants player to put his team ahead on a first career home run in the eighth inning or later since John Patterson in the ninth inning on Sept. 1, 1993, at Atlanta.

TRAINER’S ROOM

Red Sox: Brock Holt, sidelined since May 20 with a concussion, will meet the team in Minnesota and the next steps will be determined. He spent Wednesday being examined by the University of Pittsburgh’s renowned concussion experts. “He continues to show improvement but is he ready for rehab? The symptoms are there but they’re diminishing,” Farrell said.