PHOENIX — Zack Greinke got too fine, and it cost him.

The ace right-hander walked the bases loaded in the sixth inning and gave up the tying and go-ahead runs, and the Arizona Diamondbacks went on to a 5-3 loss to the San Francisco Giants.

Matt Moore struck out 11 in seven innings and Hunter Pence hit a key two-run double, helping the Giants to the series sweep.

“I thought we did a good job of getting the two runs off of him,” Arizona manager Chip Hale said of Moore. “When you have an ace on the mound you feel good about that. But it didn’t work out.”

The Giants pulled within three games of NL West-leading Los Angeles, which lost 3-0 at Miami. San Francisco leads the wild-card standings.

Moore (10-11) gave up two runs and eight hits, striking out four straight batters at one point. The Giants swept a road series from Arizona for the second time this season and sent the Diamondbacks to their sixth straight loss.

Pence, who had eight hits in the series, came up with another clutch swing against Randall Delgado in the seventh, doubling in Angel Pagan and Brandon Crawford with two out to give the Giants a 5-2 lead.

Greinke (12-6) allowed three runs and six hits in six innings. He has lost back-to-back starts for the first time since early April.

“I felt pretty good. Not perfect, but then the last inning, just missing by a little bit on just about every pitch,” Greinke said. “Getting behind and trying to make a good pitch, and just missing by little bit again. Not able to put a whole start together of good pitches.”

Denard Span, who went 30 at-bats without a hit until Saturday night, led off the game with a drive to right field. It was Span’s fifth career leadoff home run and first this season.

The Diamondbacks tied it in the second on Jake Lamb’s triple and a shallow single to center by Brandon Drury. Arizona went ahead 2-1 on Chris Owings’ double and a two-out, two-strike single from Welington Castillo in the fifth.

San Francisco rallied in the sixth. With the bases full, Brandon Belt slapped an opposite-field single into left field, and then Joe Panik’s grounder to first allowed Crawford to come home for a 3-2 Giants lead.

Castillo homered off Sergio Romo in the eighth, but that was it for the Diamondbacks. Hunter Strickland got four outs for his third save of the season.

“Seven solid innings (from Moore) allowed us to use the bullpen the way we wanted. Seven innings like that in this ballpark isn’t easy,” Giants manager Bruce Bochy said.

REMEMBERING 9/11

On the 15th anniversary of the Sept. 11 attacks, the Diamondbacks honored military members and first responders in a pregame ceremony that included a bagpipe ensemble’s music.

TRAINER’S ROOM

Giants: RHP Derek Law (elbow) is still on track to come off the disabled list Tuesday or Wednesday, Bochy said. He’s throwing to hitters on Monday.

Diamondbacks: LHP Andrew Chafin’s MRI revealed nothing more than swelling in his shoulder, so he’ll go six or seven days without throwing. Hale said he’d like to get Chafin in for a game or two before the season ends but acknowledged it might not happen. “At this point, we just want him to leave here with a healthy arm,” Hale said. “I’m not counting on anything right now.”

UP NEXT

Giants: RHP Jeff Samardzija (11-9) is 3-0 against the San Diego Padres this season. He faces them in the series opener on Monday night in San Francisco.

Diamondbacks: RHP Shelby Miller (2-11) will be making his ninth career start against Colorado when Arizona opens a series at home against the Rockies on Monday.