The Cubs squandered a three-run lead in the eighth and quickly went back ahead.
Gomes led off the next inning with a double and Madrigal followed with an RBI double. Madrigal later scored to put Chicago ahead by two, an extra run it needed after Matt Vierling's single pulled Detroit within a run.
Seiya Suzuki hit a homer for the second straight day and a three-run second inning helped the Cubs take an early lead.
Chicago went on to earn a third victory in a row, keeping its lead for the NL's final wild card, but manager David Ross and veteran players on the team know there's a lot of baseball left to be played this season.
“At the trade deadline, we didn’t win anything," Ross said. "At the end of August, we’re not going to have won anything. The goal is to win a championship and it’s a long, hard road ahead of us.”
Daniel Palencia (3-0) retired two batters and stranded two runners in the eighth to keep the score tied after Michael Fulmer gave up three runs in the inning against his former team. Mark Leiter Jr. pitched the ninth for his fourth save in six chances.
Beau Brieske (0-1) allowed two runs on three hits in the ninth.
Spencer Torkelson and Kerry Carpenter hit home runs for the second game in a row, clearing the fences on consecutive pitches in the sixth inning to cut Detroit's deficit to 4-2.
Happ hit a two-out triple and scored on Cody Bellinger's single in the eighth, giving Chicago a three-run cushion that they needed.
Javier Baez hit a two-run double in the eighth against one of his former teams.
Baez, star on the Cubs’ 2016 World Series championship team, has not panned out in two seasons with Detroit after signing a six-year contract worth $140 million.
“To see him struggle, he does shock me,” said Ross, a former teammate.
Chicago's Javier Assad allowed two runs on homers, and gave up three more hits and walked two over 5 1/3 innings. Hayden Wesneski and Jose Cuas pitched 1 2/3 innings of scoreless relief.
Detroit's Alex Faedo gave up four runs on three hits and two walks over six innings.
Suzuki has hit five homers in 13 games this month, giving him a total of 13. He cleared the fences just three times in the previous 57 games.
“He's playing phenomenal,” Ross said.
WELCOME TO THE SHOW
Detroit's Parker Meadows made his major league debut and hit a single in the sixth and said he felt like he was “on top of the world,” before missing some opportunities.
Meadows struck out for the second time in the seventh with the bases loaded for the first out in the three-run inning. He ended the game with a flyout, stranding the potential game-tying run at second.
“It was unfortunate,” Meadows said. “Things don't go your way all the time."
The 23-year-old outfielder is one of the franchise's top prospects and his older brother, Austin Meadows, is on the 60-day injured list with the Tigers due to anxiety.
“A lot of people have been looking forward to Parker getting here, and he has earned his way in every aspect of the game,” manager A.J. Hinch said. “Debuts are fun.”
The Tigers were happy that Cleveland claimed veteran catcher Eric Haase off waivers Monday, two days after they released him.
“He has been universally liked, loved and respected,” Hinch said. "It’s not a surprise that a team felt like he should be on their team. We expect him to bounce back from what has been a tough season.”
Chicago's LHP Drew Smyly (9-8, 4.92) and Detroit's RHP Reese Olson (2-5, 4.83) are expected to pitch Tuesday night in the second of the three-game series. The Tigers drafted Smyly in the second round of the 2010 draft and he pitched for them for two-plus seasons.