Boston led 3-2 when Victor Martinez drew a leadoff walk off Bailey (3-1), and Peralta followed with a line drive over the fence in left field for his seventh homer of the year.
David Ortiz homered and drove in a tiebreaking run with an eighth-inning single to put Boston up, but Drew Smyly (3-0) replaced Phil Coke for Detroit and prevented any further scoring by the Red Sox. He struck out four in two innings of relief.
Detroit improved to only 2-19 when trailing after eight innings. The Tigers are searching for answers with their bullpen, but on this night it was Boston's closer who faltered.
The Red Sox wasted a nice performance by John Lackey, who allowed two runs and seven hits in seven innings. Koji Uehara pitched a perfect eighth, but Bailey didn't get an out.
Joel Hanrahan, Boston's top choice to close, is on the disabled list.
Tigers manager Jim Leyland said before the game he would use Joaquin Benoit as his closer in light of Jose Valverde's struggles, but that point became moot. Jose Alvarez allowed two runs and five hits in five innings in his second big league start, and Detroit's Luke Putkonen got five straight outs in relief.
Then Leyland brought in Coke, the left-hander who has struggled against right-handed hitters. Coke struck out the left-handed hitting Jacoby Ellsbury to end the Boston seventh, but he walked switch-hitting Shane Victorino and right-handed hitting Dustin Pedroia to start the eighth.
The lefty-swinging Ortiz followed by pulling a base hit to right to give the Red Sox the lead.
Boston's relievers couldn't hold on. Peralta, who is having a fine season with the bat, connected off Bailey to win it.
Alvarez, who held Cleveland to a run in six innings in his major league debut earlier this month, handled Boston's lineup reasonably well, but Ortiz went deep in the fourth for his 15th homer of the season, a solo shot to open the scoring.
Jose Iglesias led off the fifth with a triple and scored on Ellsbury's single to make it 2-0.
In the bottom of the fifth, Torii Hunter's soft line drive with the bases loaded fell in for a two-run single, tying the game.
Alvarez is expected to make at least one more start for the Tigers in place of the injured Anibal Sanchez.
NOTES: Detroit scratched OF Matt Tuiasosopo before the game because of a mildly strained intercostal muscle. ... Boston manager John Farrell announced that RHP Allen Webster will start Saturday night's game against the Tigers. ... Boston LHP Jon Lester (6-4) faces Detroit RHP Doug Fister (6-4) on Friday night.