SAN FRANCISCO — Even when he lacks his best stuff, Diamondbacks ace Zack Greinke usually has enough to beat the Giants.

On Tuesday night, one big inning from his Arizona teammates helped, too.

Greinke threw six solid innings to continue his mastery of San Francisco and the Diamondbacks beat Johnny Cueto and the Giants 4-3.

“He just sucked it up and was able to get us through six,” Diamondbacks manager Chip Hale said. “It was almost like he was trying to make perfect pitches. The last couple innings he went back to just working down in the zone and getting early count outs.”

Greinke (12-4) allowed two runs — one earned — and six hits to earn his ninth win in 13 career starts against San Francisco and his first overall since Aug. 9.

Arizona’s ace also improved to 4-0 with a 1.66 ERA in six starts at AT&T Park.

“I think the first time I pitched here I gave up five runs,” Greinke said. “It’s a good place to pitch obviously. I felt I was making good pitches all game. I couldn’t strike anyone out but I was getting weak contact so it was fine.”

Welington Castillo hit a two-run single to cap a four-run fifth that chased Cueto, while A.J. Pollock singled twice and reached four times to help the Diamondbacks end a four-game losing streak to their division rival.

Daniel Hudson gave up an RBI single to Angel Pagan in the ninth but retired three batters for his second save.

The loss keeps the Giants 1 1/2 games behind the first-place Los Angeles Dodgers in the NL West.

Cueto (14-5) pitched well early but failed to make it out of the fifth for the first time this season. He allowed four runs, walked four and struck out five.

“I think the pitches caught up with him,” Giants manager Bruce Bochy said. “His command was a little off, he started missing his spots. It just got away from him.”

Pagan and Hunter Pence had three hits apiece for San Francisco.

In the fifth, Chris Owings hit an RBI triple, and after Cueto walked Greinke, Jean Segura singled in Owings. Two batters later, Castillo’s two-run single gave Arizona a 4-1 lead and ended Cueto’s night.

“Once we got something going we kept it going,” Hale said.

Brandon Crawford drove in San Francisco’s first run with a sacrifice fly in the third.

Pence and Joe Panik hit back-to-back doubles in the sixth to make it 4-2.

DEFENSIVE GEM

The Diamondbacks preserved the win by turning double plays in the seventh and eighth innings. Second baseman Segura started the inning-ender in the eighth when he fielded Panik’s grounder near second base and flipped the ball out of his glove to shortstop Owings, who threw to first to complete the double play.

CUETO’S SLUMP CONTINUES

Cueto dominated the first half but has struggled since. The NL’s starting pitcher in the All-Star game has just one win in nine starts since the break.

TRAINER’S ROOM

Diamondbacks: Chris Herrmann (strained left hamstring) continues to rehab with Triple-A Reno and Hale is confident the catcher will rejoin the big league club soon.

Giants: Span left the game with a stiff neck in the seventh. … Pence had missed the last three games with a strained right hamstring. … Jake Peavy received a cortisone shot to alleviate some of the pain in his lower back.

UP NEXT

Diamondbacks: RHP Shelby Miller (2-9) will be called up from Reno to make his 15th start of the season Wednesday in the second half of this two-game series. Miller hasn’t pitched in the majors since July 6 and his 7.14 ERA is nearly double that of any previous season.

Giants: LHP Matt Moore (8-10) pitches the finale for San Francisco. Moore lost his bid for a no-hitter with one out in the ninth inning against the Dodgers in his most recent start.