PHOENIX -- Evan Longoria's last hit was the biggest one -- a tiebreaking homer in the eighth.
He drilled Diamondbacks reliever Archie Bradley's 2-2, 97 mile-per-hour fastball with one out into the outcropping of seats in left-center field. It was Longoria's 12th home run of the season.
"You just try to get the barrel on the ball. I thought it might go," Longoria said. "The wall is pretty high out there so you have to hit it pretty high. I'm glad it just scraped over."
The Diamondbacks' loss and the Los Angeles Dodgers' win Sunday created a tie for first place atop the NL West between the two teams.
Bradley (3-3) entered the game leading the NL in holds with 30. He had not allowed a run to the Giants in 12 straight games against them.
"Archie just made a mistake to a really good hitter," Diamondbacks manager Torey Lovullo said. "I thought he threw the ball well outside of that."
Right-hander Ray Black (1-0) earned his first major league win with a scoreless seventh inning. The Diamondbacks loaded the bases in the bottom of the eighth with two outs against Giants relievers Tony Watson and Sam Dyson, but Dyson got Ketel Marte to fly out to end the inning.
Will Smith retired the Diamondbacks in order with two strikeouts in the ninth for his seventh save.
"The pitching was there and we needed it," Giants manager Bruce Bochy said.
Giants All-Star catcher Buster Posey returned to the lineup after missing Saturday's game for concussion testing. Posey played first base and reached base twice.
Both starters lasted 5 1/3 innings in no-decision efforts. The Giants' Derek Holland allowed two runs on three hits and struck out six with two walks. The Diamondbacks' Robbie Ray walked four and struck out eight, giving up two runs on seven hits.
Steven Souza Jr.'s third home run of the season sailed into the left field seats and gave Arizona a 1-0 lead in the second inning. But the Giants tied the game in the third.
Longoria, who had three hits on the day, beat the defensive shift with a two-out single to drive in Andrew McCutchen. He has reached base in eight of his last nine plate appearances.
Marte bounced a single to right field to drive in Eduardo Escobar from second base for a 2-1 Diamondbacks lead in the fourth.
The Giants had a chance for a big inning in the sixth. Longoria led off with a double and scored on Nick Hundley's single, but Hundley was thrown out by left fielder David Peralta trying to reach second base on the hit. Austin Slater followed with a single and reached third base before the inning ended with Yoshihisa Hirano striking out Gorkys Hernandez.
Ray said he was thinking about a little boy he accidentally hit with an errant throw during pregame long toss. The boy, whom Ray believed to be 4 or 5 years old, was fine and did not have to leave the stadium.
"If he wouldn't have been all right, I don't know how I would have went out there and pitched today," Ray said.
Ray and catcher John Ryan Murphy spoke to the boy and the two players signed autographs for him. "Him staying there and watching the game, that was big for me from a mental standpoint," Ray said.
The Giants finished a six-game road trip to San Diego and Arizona with a 4-2 record after earning a series split against the Diamondbacks. It's their first winning road trip this season and the first since going 4-2 at Pittsburgh and Detroit last year from June 30 to July 6.
"It has been a while for us," Bochy said. "It's good to have a little success on the road."
Giants: Posey, who left Friday's game in the third inning after he took a foul ball to his mask, could catch in Monday's game if he continues to feel OK, Bochy said.
Diamondbacks: IF Deven Marrero (strained oblique) played nine innings of a rehab assignment game with Triple-A Reno Saturday and was scheduled to play again Sunday. ... OF Jarrod Dyson (groin strain) is hitting and throwing, but not doing serious running yet.
More AP baseball: https://apnews.com/tag/MLBbaseball
Kurkjian tabs deGrom to win NL Cy Young
Tim Kurkjian explains why he is picking Mets pitcher Jacob deGrom to win the National League Cy Young Award.
Future of baseball at stake with owners, players set to meet
With a potential labor war looming, here are three talking points that could bring the two sides together when they sit down next week.
Japan beats MLB All-Stars 6-5 to clinch exhibition series
Takayuki Kai drove in the go-ahead run in the eighth inning to lead Japan over the MLB All-Stars 6-5 Wednesday and give the hosts a 4-1 lead in their six-game exhibition series.
A blockbuster offseason move for all 30 teams
Want to make a hot stove splash? Start with these bold trades and free-agent additions.
Athletics' Bob Melvin nabs AL Manager of the Year honors
Oakland Athletics skipper Bob Melvin was named American League Manager of the Year on Tuesday after guiding the team with the lowest Opening Day payroll in baseball to a 97-65 record and its first playoff berth in four years.
Brian Snitker is NL's top manager after leading Braves to East title
After leading the Braves to an unexpected National League East title, Brian Snitker has been named NL Manager of the Year. The Brewers' Craig Counsell and Rockies' Bud Black finished second and third, respectively.