PHOENIX — Eighteen straight retired, 85 pitches.

That was Nathan Eovaldi through six innings Wednesday night, the righthander still firing fastballs occasionally touching 98 mph, a splitter as good as it’s been all season, and a 3-1 lead.

But with his full complement of seed-throwing relievers available at the back end of the bullpen, Joe Girardi went to them.

It nearly blew up in his face but, after jittery moments from both Dellin Betances and Andrew Miller, the Yankees escaped with a 4-2 victory over the Diamondbacks in front of 32,191 at Chase Field to avoid what would have been an embarrassing sweep.

The 26-year-old Eovaldi, inconsistent much of the season — like every rotation member — allowed a leadoff double and nothing else over six brilliant innings, retiring 18 straight thereafter.

Only a fielding blunder by Chase Headley allowed a run to come across.

Eovaldi allowed one run and one hit over six innings, striking out five.

He gave way to Betances, who nearly had torch and pitchfork crowd looking for Girardi when he walked the first two batters, Phil Gosselin and Paul Goldschmidt, he faced.

But Betances struck out Jake Lamb, after falling behind 2-and-0, and quickly retired the side.

Andrew Miller allowed a leadoff homer to Chris Owings in the eighth to make it 3-2, but struck out three straight.

After Evan Marshall threw a wild pitch with the bases loaded with two outs in the ninth to make it 4-2, Aroldis Chapman struck out one in a perfect ninth to notch his fourth save in four tries.

Arizona righthander Shelby Miller, who came in 1-4 with a 6.94 ERA and having walked more batters (23) than he’s struck out (22), allowed three runs and 10 hits over 5 2/3 innings.

Jacoby Ellsbury, who went 3-for-3 with two walks, led off the game with a walk and came in on Brett Gardner’s fifth homer of the season, which made it 2-0.

After Carlos Beltran grounded out to first, Brian McCann walked. Mark Teixeira, hitting just .158 against righthanders this season and one of four players managing general partner Hal Steinbrenner singled out earlier in the day at the quarterly owner’ meetings as playing a significant role in the Bombers’ poor start, ripped one to right where Brandon Drury made a leaping catch while crashing against the wall. Starlin Castro grounded to third to end the 24-pitch inning.

“I don’t blame him,” Teixeira, who brought a .211/.318/.313 slash line into the game in which he went 0-for-5 with three strikeouts, said of Steinbrenner’s comments in which he also mentioned Chase Headley, Michael Pineda and Luis Severino. “I’ve been terrible the last month.”

Eovaldi had some bad luck and poor fielding go against him in the bottom half as the Diamondbacks (19-24) cut it to 2-1.

Jean Segura, 4-for-9 the first two games of the series, led off with a slow bouncer up the middle that bounced off the second base bag and into center for an interesting-looking double. Gosselin’s groundout to second moved Segura to third and Paul Goldschmidt, 3-for-7 with a homer and three runs the first two games, followed with a grounder to third. Segura hesitated going down the line and almost seemed surprised when Headley didn’t give him more than a cursory glance back. Segura continued home and scored as Headley nipped Goldschmidt at first.

The Yankees, who stranded six over the first five innings, finally added on in the sixth on Ellsbury’s two-out RBI single that brought in Headley to make it 3-1.