Jordy Mercer’s two-run homer in the bottom of the ninth gave the Detroit Tigers a 10-8 victory over Kansas City Royals on Thursday night.

Travis Demeritte led off the ninth with his third walk of the game and moved to second on a sacrifice bunt by Jake Rogers.

Mercer then hit a 2-1 fastball from Richard Lovelady (0-2) into the right-field stands for his sixth homer of the season and the eighth of a game in which both starters struggled.

Joe Jimenez (3-6) got the win with a scoreless ninth inning.

Tigers starter Matt Boyd gave up five runs on six hits and three walks in a season-worst 2 2/3 innings. Boyd entered the game fourth in the American League with 187 strikeouts, but only had one against the Royals.

Hector Lopez, making his first start since May 25, only got four outs. He allowed five runs on two hits, three walks and a hit batter.

The Royals took a 2-0 lead in the first. Whit Merrifield led off the game with a double and scored on Hunter Dozier’s one-out homer to right.

Detroit, though, scored five runs on two hits in the second.

After a single and an error, Harold Castro’s three-run homer gave the Tigers the lead.

Lopez walked the next two hitters and, after a groundout, hit JaCoby Jones to load the bases. He then walked Niko Goodrum on four pitches to force in Detroit’s fourth run of the inning before being replaced by Kevin McCarthy.

McCarthy walked Miguel Cabrera to make it 5-2 before getting out of the inning.

Dozier led off the third with his second homer of the game, and after Jorge Soler walked, Cheslor Cuthbert hit his eighth homer over the Royals bullpen in left-center field.

Boyd retired the next two batters but walked Nick Dini, bringing Matt Hall in from the bullpen.

Kyle Zimmer replaced McCarty to start the third and walked the first three batters he faced. Mercer struck out, but Victor Reyes bounced into a run-scoring force at second to give the Tigers a 6-5 lead.

Cabrera led off the fourth with his 473rd career homer to make it 7-5, but Gordon hit a three-run homer over the scoreboard in right-center to put the Royals ahead 8-7 in the sixth.

Demeritte hit his first major league homer to tie it in the seventh.