PHOENIX, Ariz. — Don’t let the little things get to you.

That was the mantra that surrounded the Arizona Diamondbacks Sunday afternoon as they overcame a bit of bad luck to knock off the San Francisco Giants 3-2 in 10 innings.

Behind starting pitcher Merrill Kelly’s impressive outing and Tim Locastro’s walk-off single to seal the deal, Arizona ended its six-game skid in a big way, avoiding a second consecutive home sweep.

The win couldn’t have come at a better time for manager Torey Lovullo and the D-backs.

“We’ve been fighting and grinding and we haven’t had good circumstances and I’ve come in here talking about losing a baseball game five straight days and that’s hard,” Lovullo said after the game. “It’s hard for me, it’s hard for the team, it’s hard for everybody, but we’ve got to find a way to keep grinding to have these moments happen so it’s exactly the opposite of what I’ve been feeling for the last couple days.”

Kelly continues to be one of the most consistent pitchers in Arizona’s starting rotation this season.

In six innings of work (99 pitches/63 strikes), the pitcher allowed just two earned runs on four hits while fanning nine batters and walking just two. He’s had just one outing go less than four innings this season and is now tied with ace Zack Greinke in the win column with an 8-7 record.

“I was just happy that I gave the team a chance to fight and that’s exactly what they did,” Kelly said of his performance. “These guys did a great job fighting and picking me up and getting a huge, huge victory to snap the losing streak, get some good momentum going, some good mojo going, so I was happy I was able to keep us in that position to eventually win.”

It’s the third game in which Kelly got the victory with the offense only mustering just three runs or less.

And as good as his line reads, it was his worst inning of the day that really helped keep the D-backs in it.

After only allowing a leadoff double and a walk in the first 11 batters he faced, Kelly ran into a tough fourth inning.

Evan Longoria started off the fourth with a single before a double from Brandon Belt put the two in scoring position with no outs. The very next batter, catcher Stephen Vogt, grounded out but not without scoring a run, tying things up at 1-1.

But instead of crumbling under pressure, Kelly dug in, recording a Kevin Pillar strikeout before getting Brandon Crawford to groundout to first, stranding Belt on third to end the inning still tied, 1-1.

“He stepped on his stuff there, I thought that big out of Pillar was the big moment, Pillar’s been wearing us out the last couple days,” Lovullo said of Kelly’s fourth. “That was damage control that we talk about. He kept us right there in the ballgame when things didn’t escalate and there was no crooked numbers.

“Huge moment, and you can back up the game four or five times any given night when a starting pitcher does something like that, it’s a huge pick-me-up.”

“We’ve been struggling, obviously six [losses] in a row, but I’m just glad it wasn’t seven and I think that fourth inning contributed to that eventual win,” Kelly added.

But the victory wasn’t without its moments of overcoming adversity for Arizona.

In the seventh inning, first baseman Christian Walker smacked a ball to the outfield that jumped into the stands for a ground rule double. The drive would have surely scored Eduardo Escobar who was held up at third after the ruling. At the time, it was a 2-2 game with no outs.

Then things took another turn.

With runners at second and third and no outs, Arizona looked to be in the driver’s seat, until a wild pitch from San Francisco reliever Reyes Moronta.

Moronta’s pitch was well off, getting by Vogt, but here’s where it gets weird.

Instead of bouncing around behind home plate, the ball ricocheted back to the catcher who hardly had to move to tag a running Escobar for the first out of the inning.

Yes, a wild pitch from the opposing team was crucial in why the D-backs didn’t head to the eighth with at least a one-run lead.

Nick Ahmed followed it up with a groundout before a Domingo Leyba strikeout ended the D-backs’ chances.

“A lot of little things could have shut us down, but I’m really proud of the way we hung tough, fought and won this game,” Lovullo said of his team. “And that’s the bonus for not shutting things down.

“They find a way to do things the right way every single day. And that’s what I’m proud of.”

From there, both teams’ bats went quiet, not recording a hit through the ninth inning.

Then Walker stepped up to the plate in the 10th.

Walker, who’s found his hitting stroke after going through a slump earlier in the season, needed only three pitches to get on base, hitting a leadoff single. With his three-hit, one-RBI day, Walker extends his hitting streak to eight games.

Ahmed was next up, but failed to advance Walker. Even so, the D-backs still had a baserunner. That was followed up by walks of Jarrod Dyson and Carson Kelly, making way for outfielder Locastro.

Locastro, who is considered a “ball magnet” by most due to his uncanny ability to be hit by pitches, was the one who did the hitting this time, knocking a liner just over the outstretched hand of Longoria at third base to bring in Ahmed.

“I wanted to get a hit,” Locastro said when asked about not getting hit on the first pitch of the at-bat. “We would have won either way but I wanted to get a hit there. … We were able to get the win today and now we need to get on a streak here and really get going so that’s the goal moving forward.

“As a team, top to bottom, the starting pitching did such a great job there and then the whole lineup did a good job there and being able to come through like that and get an exciting win, that’s something you can build off of. That’s what we’re hoping to do tomorrow.”

Sunday’s win marked just what the D-backs needed.

Entering the matchup, Arizona had lost its last six games, including five at home. All told, the D-backs now sit with a 15-21 record at home, while they boast a 24-19 road record.

The victory comes at the right time as the NL-West leading Los Angeles Dodgers come into town for a three-game series starting Monday.

“We know they’re pacing the way in the West and we know they’re having a great season,” Lovullo said when asked what the win does for the team with the Dodgers next up.

“We want to stand up in the middle of the ring and start it up and I can’t imagine us feeling any better tonight, walking into the field tomorrow to prep us for that moment.”

Tune in to 98.7 FM Arizona’s Sports Station to catch all the action of Monday’s game.