New York trailed 5-3 with the bases loaded and one out before Pittsburgh's Colin Holderman (0-3) walked Estevan Florial to force in a run.
Anthony Volpe then grounded into what should have been a game-ending double play. Shortstop Liover Peguero fielded a grounder and threw to Ji Hwan Bae for a forceout at second, but Bae's throw bounced past first base for an error as two runs scored to put the Yankees up 6-5.
After Oswald Peraza singled, Oswaldo Cabrera's single made it 7-5.
Earlier, Ben Rortvedt, DJ LeMahieu and Aaron Judge hit RBI singles for the Yankees (75-73). New York won for the fifth time in six games.
New York starter Gerrit Cole allowed two runs and six hits in five innings. He walked three and struck out four. Nick Ramirez (1-2) pitched a scoreless 1 1/3 innings, and Clay Holmes handled the ninth for his 20th save.
Yankees reliever Anthony Misiewicz was taken off the field on a cart after he took a line drive to the head in the sixth. He was alert at a local hospital, the team announced.
Ke'Bryan Hayes hit a two-run single and Bryan Reynolds and Ji Hwan Bay added RBI singles for the Pirates (69-79), whose three-game winning streak ended.
Pittsburgh starter Johan Oviedo held the Yankees to one run and four hits in five innings. He issued five walks and fanned seven.
Jack Suwinski's sacrifice fly in the first gave Pittsburgh a 1-0 lead.
The Yankees tied it in the second. Volpe doubled, went to third on Peraza's groundout and scored on Rortvedt's single.
In the bottom of the second, Peguero singled and later scored on Reynolds' single for a 2-1 Pirates lead.
In the sixth, Cabrera singled, Rortvedt walked and LeMahieu singled to drive in Cabrera to tie it. Judge's base hit brought Rortvedt home to make it 3-2.
In the bottom of the sixth, Miguel Andujar walked and went to second on Joshua Palacios' single. An out later, Bae hit a liner that caught Misiewicz in the head and caromed into right, driving in Andujar for a 3-3 tie.
Zach McAllister took over on the mound, and Reynolds was hit by a pitch to load the bases. Hayes singled, driving in Palacios and Bae for a 5-3 Pittsburgh lead.
—Field Level Media