PITTSBURGH _ The Padres appeared headed to quick and certain doom on Sunday afternoon.
Jordan Lyles had faltered late in his start, giving up a lead.
Then Kirby Yates allowed a rare run in the eighth inning that put the Padres down a run, and the Padres were 0-24 when trailing after eight innings this season.
But then Jose Pirela and Christian Villanueva began the ninth inning with singles off Pirates closer Felipe Vazquez. Pirates shortstop Jordy Mercer made an error on a Cory Spangenberg grounder that allowed the tying run to score. Freddy Galvis executed the Padres' second successful squeeze bunt of the game to give the team the lead. Spangenberg and Galvis pulled off a double steal, and A.J. Ellis drove them both home with a single.
Brad Hand pitched a scary but scoreless ninth and earned his 14th save in closing out an 8-5 victory.
With that, the Padres had won their third straight game and taken three of four from the team that led the National League Central three days earlier.
The Padres, who entered May with a 10-20 record, are 10-8 this month.
Their latest victory was the least probable of all.
Lyles, in his first outing since throwing 7 1/3 perfect innings against the Rockies on Tuesday, was one out from getting through the sixth inning with a three-run lead on Sunday when he gave up home runs on back-to-back pitches that tied the game at 4.
Then the most reliable of the Padres' reliable relievers faltered
The Pirates scored a run on two singles, a hit batter and a sacrifice fly in the eighth, the first run allowed by Yates in 11 appearances (10 1/3 innings).
The Padres entered the ninth not only with their record of final-inning futility, but having been retired in 11 straight at-bats stretching back to Lyles' RBI on a suicide squeeze that scored Ellis in the sixth inning.
That run had put the Padres up 4-1 and gave them back-to-back two-run innings.
Eric Hosmer's single through the hole at shortstop with one out in the fourth inning was the Padres' first hit off Pirates starter (and Rancho Bernardo High alumnus) Trevor Williams. Two batters later, Villanueva hit his second homer in two games and 12th of the season to give the Padres a 2-1 lead.
Successive doubles by Galvis and Ellis started the fifth. Ellis' blast off the wall in right field appeared as if it might have been caught, which forced Galvis to hold up and only advance to third. Manuel Margot's grounder to second scored Galvis and moved Ellis to third.
Then, just before Williams let go of a 2-1 pitch to Lyles, Ellis took off from third. Lyles laid down a bunt on the grass in front of the mound, and Ellis just beat the underhand toss home.
Williams would get through the sixth, allowing the four runs on four hits.
With help from two walks and a misplay in the field, the Pirates loaded the bases with two outs in the ninth before a line out to Spangenberg at third base ended the game.