NEW YORK — Travis Hafner had a pinch-hit homer in the eighth inning to make a winner of CC Sabathia, who settled down after a shaky start and helped the New York Yankees beat the Arizona Diamondbacks 4-3 Wednesday night.

Brett Gardner had a tying, two-run single in the seventh for New York. Stymied by Wade Miley for six innings, the Yankees broke through and won for the seventh time in eight games.

Sabathia (3-1) gave up an opposite-field, two-run homer to Paul Goldschmidt in the first — one of three hits in the opening inning — then only three more hits and a sacrifice fly to A.J. Pollock through the eighth.

Hafner batted for Ben Francisco with two outs in the eighth. He sent the first pitch he saw from reliever David Hernandez (0-1) into the stands in right-center for his fourth home run. It was the fourth career pinch-hit for Hafner, and his first since joining the Yankees in the offseason.

Sabathia struck out six and walked one in his third straight win after struggling on opening day. It was his second straight outing of eight innings.

Mariano Rivera pitched the ninth for his fourth save, closing out a game that took just 2 hours, 28 minutes. Injured for most of last season, Rivera posted his first back-to-back saves since April 10-11, 2012.

The Yankees rallied from an early 2-0 deficit for the second straight night.

New York failed to get a runner to second base against Miley until Vernon Wells doubled with two outs in the sixth.

Francisco, the only member of New York’s lineup to face the left-hander before Wednesday, singled with one out in the seventh to start the tying rally.

After Francisco Cervelli struck out, Brennan Boesch doubled and Eduardo Nunez walked to load the bases. Calm and working quickly early, Miley took the throw back after the walk and stomped around the mound. He then walked Jayson Nix to make it 3-1 with several high fastballs.

Miley snatched the ball and stalked around the mound some more before manager Kirk Gibson called for Tony Sipp. Miley bit down hard on a towel in the dugout while Sipped warmed up.

Gardner then slapped a two-run, opposite-field single to left to make it 3-all. The runners advanced to second and third on the offline throw home and side-armer Brad Ziegler relieved and got Wells to ground out.

The Diamondbacks got off to a good start against Sabathia. Pollock doubled leading off and Goldschmidt homered. They put two more on base with one out, but Sabathia struck out Alfredo Marte and got Gerardo Parra to fly out.

The Diamondbacks added a run in the fifth. Playing second base for the injured Aaron Hill, Josh Wilson hit a drive to right-center and Gardner made a long run before attempting an awkward diving catch.

But the ball fell out of Gardner’s big glove before he hit the warning track hard and Wilson made it to third base with a triple.

He scored with a headfirst slide on Pollock’s sacrifice fly to right field.

Francisco’s one-out single in the seventh was his first hit with the Yankees. He was 0 for 10 with a walk and hit by pitch.

Martin Prado, who’s played four positions so far this season, made two sharp plays at third to help Miley early.

NOTES: Yankees 1B Mark Teixeira and Cano received their 2012 Gold Gloves before the game. ... Arizona LHP Patrick Corbin (2-0), from the Syracuse area in upstate New York, starts Thursday against RHP Phil Hughes (0-2).