Kansas City Chiefs rookie quarterback Patrick Mahomes had to beat the Denver Broncos twice in the second half of their Week 17 matchup.

After leading his team to a 14-point lead in the fourth quarter, Mahomes sat on the sideline and watched as quarterback Tyler Bray and the Kansas City coughed up the lead by allowing a scoop-and-score fumble, then going three-and-out leading to a Denver touchdown.

Mahomes returned to the game to lead one final scoring drive, though, and escape Denver with a 27-24 victory in his first NFL start.

Kansas City ends the season 10-6 and will give the ball back to Alex Smith in the first round of the playoffs as AFC West champions. Denver, meanwhile, enters a period of uncertainty with their head coach and quarterback after a 5-11 campaign in Vance Joseph‘s first year at the helm.

Second-year quarterback Paxton Lynch was inconsistent throughout the middle of the game, throwing two interceptions and allowing a fumble to be returned for a touchdown, bookended by touchdown drives in the second and fourth quarters.

After a back-and-forth first half that had the Broncos trailing 14-10, Lynch and the Denver offense had the first opportunity in the second half. The second-year quarterback leaned heavily on a of veteran in pursuit of 1,000-yard seasons early in the drive. A pair of C.J. Anderson runs led to a first down. A six-yard pass to Virgil Green followed. But a blown-up delayed draw to Anderson on second down put the Denver offense in a tough spot. Riley Dixon eventually punted and the Broncos coverage team pushed Demarcus Robinson back four yards to the KC eight-yard line.

Von Miller pressured Mahomes on second down to force a quick third-and-eight, but the rookie hit Albert Wilson for the fifth time in the game to escape a three-and-out. Demetrius Harris picked up another 11 yards with a catch on the far sideline on the very next play. Mahomes continued to spread the ball around and show his arm strength on the drive, throwing on the run to hit Demarcus Robinson for seven yards. Harris and Mahomes connected for another 11 yards as the drive moved into Denver territory. Mahomes escaped a third-down pass rush by Shaquil Barrett and hit fullback Anthony Sherman on a screen for seven yards to set up fourth-and-one from the Broncos’ 29-yard line.

Sherman, who scored a go-ahead touchdown in the first half, got the call again on fourth down and picked up five yards for the first down. Mahomes went right back to the air, hitting Robinson for 10 yards and another first down. It was Sherman again on a screen pass to set up first-and-goal. Denver took a too-many-men penalty on second and goal, but Adam Gotsis got through the KC offensive line and pushed Sherman back for a loss of one. A quick-strike attempt from Mahomes to Robinson was a bit too early for the wide receiver and KC was forced to kick a field goal, extending their lead to 17-10. The 90-yard drive consumed 8:46 of the third quarter.

For all the poise and situational awareness that Mahomes showed on Kansas City’s first drive of the half, Lynch showed on the second play of Denver’s second drive why he has struggled to see the playing field in his first two seasons in Denver.

After a lengthy injury stoppage for an injured Chiefs player, Lynch dropped back in play-action only to be consumed by the Kansas City defensive line. When the play was finished, Lynch had fumbled and Ramik Wilson had returned the ball 11 yards into the end zone. Lynch failed to unload the ball, then failed to protect it and suddenly Kansas City had a commanding two-touchdown lead, 24-10.

Lynch again suffered the wrath of the Chiefs’ D-line on the next drive, taking a sack on third-and-six after converting a quick first down. The difference in pocket awareness between Lynch and Mahomes was becoming evident with each drive. After putting up points on two of his first four drives in the game, Lynch went interception, punt, fumble (touchdown) and punt over his next four.

Mahomes, meanwhile, continued to pick apart the Denver secondary when he could and live to play another down when necessary. On the first play of the fourth quarter he connected with Wilson for the seventh time; the 19-yard pass put Wilson over 100 yards for the game. Three plays later Mahomes rolled right and evaded a heavy Denver third-down pass rush, throwing the ball away and turning things over to his punter.

In one of the few offensive positives of the second half for Denver, C.J. Anderson took the first two handoffs of the next drive for 17 total yards to give him 61 yards on the day and 1,007 on the season. It was Denver’s first 1,000-yard rusher since Knowshon Moreno reached the mark in 2013 and the first such accomplishment of Anderson’s career.

Jordan Taylor nearly made an incredible, acrobatic catch on the near sideline on the next play of the drive but his second foot came down just barely out of bounds. The play was initially ruled a catch, but overturned on a challenge by Kansas City head coach Andy Reid.

Lynch did show some toughness in the pocket later in the drive, connecting with Bennie Fowler while being hit by Jarvis Jenkins. The play was ruled roughing the passer, giving Lynch and the offense another 15 yards on the play. That was undone when the offensive line broke down later in the drive, however, as Lynch took an 10-yard sack on first-and-10 from the Chiefs’ 11-yard line. On third down, Lynch scrambled for five yards with nothing doing downfield.

With nothing to lose in the season and already trailing by two touchdowns, Joseph asked his young quarterback to make a play on fourth-and-15. For the third time in the game Lynch turned the ball over, this time being intercepted by Ron Parker at the two-yard line. Parker would return the ball 40 yards, prompting Reid to hand the quarterbacking duties over to Tyler Bray.

That proved unfortunate for Kansas City, as Bray botched a handoff to Sherman and Denver’s Zaire Anderson picked up the mistake and took it 38 yards to the end zone to cut the deficit to seven at 24-17. It was Bray’s first regular-season NFL snap after going undrafted in 2013.

His second was more successful, a handoff to Sherman, but he could do little else on the next drive as the Chiefs quickly went three-and-out.

Lynch connected with Bennie Fowler for 22 yards as the clock wound down below 5:00, showing some life against the Kansas City second unit. He hit running back Devontae Booker wide open on an uncovered wheel route for a pickup of 25 yards and a first-and-10 from the Chiefs’ 11-yard line. Demaryius Thomas dropped a pass at the three to set up third-and-four. Kansas City cut down De’Angelo Henderson for no gain trying to find the edge and force a fourth-down try.

Lynch earned some redemption, giving himself time to find Thomas who had broken free in the end zone, and suddenly the Broncos had tied the game at 24.

With 2:45 remaining after the kickoff, Andy Reid went back to Mahomes at quarterback to try and win the game.

Rookie defensive lineman DeMarcus Walker picked a great time to pick up his first NFL sack, getting to Mahomes on the first play of the ensuing drive for a loss of seven. Just as he had done all game, though, Albert Wilson hurt the Broncos — this time with a 13-yard gain to give Mahomes a chance on third down. He missed deep to Robinson, but it didn’t matter because Todd Davis jumped offside and gave Kansas City an automatic first down.

Mahomes again showed why the Chiefs may be tempted to move on from Alex Smith soon when he was flushed from the pocket but still managed to connect with Robinson for 12-yards, throwing across his body under heavy pressure. He connected with Wilson for another 13 yards on the next play. After an ill-advised throw deep into double-coverage, Mahomes again hit Wilson underneath, this time for a gain of 14 yards and a first down with under 1:30 to play. Coming out of a timeout and now in field-goal range, Kansas City ran an option toss out of bounds, then a quarterback draw that forced Denver to use their final timeout.

The fullback Sherman got the call on third-and-four, going for seven yards and a first down. He went for another four yards on the next play as the clock wound down to :03 and Kansas City used their final timeout to give Harrison Butker the chance for a game-winning field goal.

The rookie kicker was true from 30 yards as time expired to end the game 27-24.

The loss closes Denver’s first losing season since 2010, a year that cost former Broncos head coach Josh McDaniels his job midway through the season. The same fate reportedly awaits Vance Joseph.

What the future holds for Lynch is still to be determined. Denver’s quarterback carousel that also included Trevor Siemian and Brock Osweiler this season will continue to turn as free agency and the draft loom. General manager John Elway will have many options this offseason to fix the first losing team in his tenure as the head of football operations.