Jorge Soler broke the Kansas City home run record with his 39th homer of the season and Ryan O’Hearn hit a game-ending home run to lift the Royals to a 6-5 win over the Detroit Tigers on Tuesday night.

O’Hearn sent the first pitch he saw from Zac Reininger in the ninth inning over the fence in right field after a seesaw final three innings.

Tigers’ starter Daniel Norris grooved Soler a 2-2 fastball in the third inning and the Royals’ slugger didn’t miss. The three-run shot broke a tie with Mike Moustakas, who set the previous record with 38 home runs in 2017. The home run also gave Soler 100 RBIs on the season, becoming the first Royal to cross the century mark since Eric Hosmer in 2016.

Mike Montgomery was solid once again, allowing one run on six hits in 5 2/3 innings. Montgomery has now allowed one earned run or fewer in four of his last five starts and has a 2.10 ERA and 30 strikeouts in 30 innings over that span.

Whit Merrifield had three hits, upping his season total to 176, just one back of Boston’s Rafael Devers for the MLB lead.

The Tigers scored four runs from the seventh inning on to twice rally from deficits. Brandon Dixon put Detroit up 4-3 in the eighth inning with a sacrifice fly to score Jeimer Candelario, then tied the game in the ninth on Ronny Rodriguez’s single, but Alex Gordon threw out Willi Castro at home to end the inning.

Gordon tied the game in the bottom of the eighth, legging out an infield single and Soler put the Royals up 5-4, racing home on a wild pitch.

Ian Kennedy (2-2) blew his fourth save of the season, but got the win thanks to O’Hearn.

Soler’s home run was the only blemish on Norris, who threw just three innings because of pitching limits set by Detroit pitching coach Rick Anderson. Norris has thrown exactly three innings in five consecutive starts now.

Playing in his first major league game since June 13, Grayson Greiner was at the center of the Tigers’ offense from the No. 9 hole in the lineup. He laced an RBI single in the third inning to put Detroit up 1-0, then scored on Victor Reyes’ RBI double in the seventh inning to bring Detroit with in one, 3-2.

Greiner finished with three hits and Reyes had a career-high had four hits, including two doubles, for his fifth straight multi-hit game. Reyes is hitting .591 (13 for 22) in those games.