The Tigers, last in the majors in home runs, connected five times and ended a five-game losing streak.
Giancarlo Stanton hit his 300th career home run for the Yankees, who have lost three of four. They began the day 7 1/2 games behind Boston in the AL East and 4 1/2 games ahead of Oakland for the top wild-card spot.
Luke Voit hit a two-run homer in the seventh that put the Yankees ahead 7-5.
But Betances (4-4) walked Nicholas Castellanos with one out in the ninth and Martinez lined a drive to right for his 12th career multihomer game.
Goodrum followed with a flyball that curled around the foul pole. Betances had allowed a total of two runs in his past 35 appearances dating to late May, and had never given up two homers in a game.
Alex Wilson (2-4) earned the win with a perfect eighth and Shane Greene recorded his 28th save.
Martinez hit a two-run homer in the fifth.
Jeimel Candelario led off the game with a home run against J.A. Happ, and Ronny Rodriguez also homered for the Tigers.
Gleybar Torres had a two-run homer for the Yankees off Francisco Liriano.
Stanton's two-run drive in the third came in his 1,119th game — only Ralph Kiner, Ryan Howard, Juan Gonzalez and Alex Rodriguez have hit No. 300 faster.
IN THE 100-WIN CLUB
Happ (107 wins) and Liriano (105 wins) are 19th and 20th among active pitchers in wins. Their matchup marked the 25th time this season a pair of pitchers with 100 wins opposed each other. Liriano has been involved in five such games while Happ has been in three.
Tigers: SS Jose Iglesias (abdomen) was placed on the 10-day disabled list. INF Dawel Lugo, one of the players the Tigers acquired from the Arizona Diamondbacks in the J.D. Martinez trade last year, was recalled and went 1 for 4 while starting at shortstop in his major league debut.
Yankees: C Gary Sanchez (groin) went 0 for 4 while catching all nine innings at Triple-A Scranton/Wilkes-Barre. The Yankees hope Sanchez, who has been out since July 24, can be activated Saturday. . SS Didi Gregorius (heel bruise) rested Thursday after hitting and taking ground balls Wednesday. . OF Aaron Judge (broken right wrist) ran and played catch but has yet to swing a bat.
Tigers: RHP Jordan Zimmermann (6-6, 4.38 ERA) has received a decision in 10 straight starts. He is 2-3 this month with an 0.73 ERA in his wins and a 6.75 ERA in his losses.
Yankees: Major league wins leader RHP Luis Severino (17-6, 3.27 ERA) will look to become the Yankees' first 18-game winner since 2011, when LHP CC Sabathia finished with 19 victories.