New York faced a 3-0 deficit heading into the seventh, but put together a two-out rally to tie it in that inning and then grabbed the lead on Hafner's blast in the eighth.
The Yankees loaded the bases with two away in the seventh against Wade Miley, who then walked Jayson Nix to force in a run. Tony Sipp relieved Miley and promptly gave up a two-run single by Brett Gardner.
CC Sabathia (3-1) benefited from the comeback to get the win, allowing three runs on six hits with six strikeouts in eight innings. Mariano Rivera worked a perfect ninth for his fourth save and brought the Yankees their third straight win.
David Hernandez (0-1) suffered the loss after giving up the homer to Hafner.
Paul Goldschmidt accounted for two of Arizona's runs with a first-inning homer.