Mike Yastrzemski and Austin Wynns also went deep as San Francisco (79-78) moved above .500 for the first time since Aug. 17.
The Giants also kept their slim wild-card hopes going with five games remaining, though a loss or a win by Philadelphia will eliminate them.
The timing of the Giants' hot streak proves frustrating for right-hander Alex Cobb (7-7), who picked up the victory with five innings of four-run ball.
''It's been great, (but it's) a little late,'' Cobb said. ''That's all I've really got to say about it. It's been good baseball, just too late. If we just stopped some of those skids we had earlier, we wouldn't be in this position.''
San Francisco is trying to become the third team in major league history to finish at or above .500 after being eight games under with 16 or fewer games to play. The last team to accomplish that was the 1925 Cardinals.
Merrill Kelly (13-8), who came in 3-0 with a 1.53 ERA against the Giants this season, served up three homers and was tagged for eight runs in 4 2/3 innings.
''I think he never found a rhythm, and he certainly made some middle-middle mistakes,'' Arizona manager Torey Lovullo said. ''When you make mistakes in the zone, and that happens from time to time, you're going to give up some runs.''
Longoria, who has missed a combined 54 games over three stints on the injured list, hit a three-run drive in a four-run first. The next inning, he delivered a two-run homer to center. Twice this season, he's homered in both the first and second innings of a game. He's the first player in franchise history to do that twice in the same season.
Before Longoria's homer in the second, Joc Pederson took a wild ride around the bases, leading off the inning with a triple that ricocheted off the right- field wall and bounced back toward the infield. He came home to score on the play when Josh Rojas' throw from the outfield skipped past the cutoff man.
Pederson put his head down after he made contact, not thinking the ball would carry like it did in the crisp air at Oracle Park. He began running hard after the ball caromed off the wall.
''He's been one of our best all year,'' Longoria said of Pederson. ''Just seeing him running around like that -- ran right through the stop sign, didn't care -- and was safe. It was fun to watch.''
WILLIE MAC WINNER
INF Wilmer Flores was honored before the game as the Giants' 2022 recipient of the Willie Mac Award, given annually to the player who best exemplifies the ''competitive spirit, ability and leadership'' of Giants Hall of Famer Willie McCovey. The award is voted on by Giants players, coaches, staff and fans.
Flores addressed the home crowd during a pregame presentation and got to catch the ceremonial first pitch from his father, Wilmer Sr. He entered the night hitting just .231, but a sacrifice fly in the sixth gave him a team-high 71 RBIs.
Diamondbacks: INF Ketel Marte was placed on the injured list for an unspecified reason before Friday's game, ending his season. Marte originally was in the lineup at second base, but Rojas shifted from third to second and Emmanuel Rivera started at third. Marte, who turns 29 later this month, hit .240 with 12 homers and 52 RBIs in 137 games. INF Buddy Kennedy was recalled from Triple-A Reno to take his roster spot.
RHP Drey Jameson (2-0, 0.98) goes Saturday for Arizona in his fourth career start. He's allowed just two runs over 18 1/3 innings in his first three outings. The Giants have yet to announce their starter.
More AP MLB: https://apnews.com/hub/MLB and https://twitter.com/AP-Sports
Copyright 2022 STATS LLC and Associated Press. Any commercial use or distribution without the express written consent of STATS LLC and Associated Press is strictly prohibited.