MIAMI GARDENS, Fla. — Andy Reid finally did it.

After 21 years as a head coach, after 14 years of heartache with the Eagles, other close calls with the Chiefs, and a life devoted to football, Reid won his first Super Bowl as the top guy when Kansas City mounted a dramatic comeback to topple the pesky 49ers, 31-20, Sunday night.

The Chiefs scored 21 unanswered points in the fourth quarter after spotting San Francisco a 20-10 lead. Reid, who had drawn the wrath of Philadelphia for his snail-like play-calling 15 years ago in Super Bowl XXXIX, unleashed his superstar quarterback Patrick Mahomes in a dazzling final nine minutes.

Mahomes, who had tossed two interceptions in the third quarter, drove the Chiefs on two touchdown-scoring drives to retake the lead. Some of the throws were breathtaking, two of them touchdowns to tight end Travis Kelce and running back Damien Williams.

Mahomes was named MVP of Super Bowl LIV. But it was Williams who drove the final nail into the 49ers’ coffin when he bolted 38 yards into the end zone for the final tally. The tailback, who had wrestled the lead job from former Eagle LeSean McCoy midseason, rushed 17 times for 104 yards and a score.

As the final seconds ticked down, many fans at Hard Rock Stadium — Eagles owner Jeffrey Lurie among them — chanted, “Andy, Andy, Andy.” When the clock read 0:00, Reid was engulfed by his assistants. — his son, Brett, the Chiefs linebackers coach, among them.

“We never lost faith. I mean, that’s the biggest thing,” Mahomes said. "Everyone on this team, no one had their head down and we believed in each other and that’s what he preached all year long. And we had this guy right here [Reid] to get it in the end and we found a way to get it in the end.”

Reid, at age 61, becomes the fifth-oldest head coach to win the Super, and the third-oldest, behind Dick Vermeil and Pete Carroll, to win his first title. The Chiefs won their second Super Bowl in franchise history and their first in 50 years. The Chiefs topped the Vikings, 23-7, in Super Bowl IV.

Mahomes had a difficult third quarter, but the 24-year-old was spellbinding down the stretch. He finished with 286 yards through the air on 26-of-42 passing with two touchdowns and two interceptions.

“Coach told me after both of [the interceptions], ‘Keep firing. Keep believing in your eyes, keep throwing it,’ " Mahomes said. “It gives me the confidence to go out no matter what I do, and it worked out well in the end.”

San Francisco coach Kyle Shanahan, who was the offensive coordinator when the Falcons coughed up a 28-3 lead to the Patriots in Super Bowl LI, let another one get away.

The 49ers had the opportunity to put the game away, or least far enough so that the Chiefs would need a near-miracle to come back. They had the ball with under 10 minutes left, ahead, 20-10, after Mahomes’ second interception. But they were forced to punt, and the Chiefs had life.

Reid had his offense shift to urgency mode, something he didn’t do 15 years ago with the Eagles under similar circumstances against the Patriots. A 44-yard Mahomes heave to Tyreek Hill on third- and-15 jolted the Chiefs. And after Travis Kelce was interfered with in the end zone by 49ers safety Tarvarius Moore, the tight end caught a 1-yard touchdown pass.

Kansas City had trimmed the 49ers’ lead to 20-17 with 6 minutes, 13 seconds left. And after San Francisco went three and out — a third-down blitz forced an errant Jimmy Garoppolo pass — the Chiefs had the ball back with 5:10 remaining.

The Mahomes the NFL had fallen immediately in love with had returned. The Chiefs needed only seven plays to march 65 yards down the field. Mahomes was involved in every play. He completed 5 of 5 passes for 60 yards and ran twice for 5 yards.

The capper was a dump to Williams out of the backfield. He angled for the pylon and reached out the football just over the plane before stepping out of bounds. The Chiefs had scored 14 straight points to retake the lead, 24-20, with 2:44 to go.

The 49ers drove to the Chiefs 49, but that’s as far as they would go. With Garoppolo asked to shoulder the load, Shanahan’s run-heavy offense had stalled.

Reid and Shanahan were both brilliant and a step ahead of their defensive coordinator counterparts for most of the night. For the latter, it didn’t matter the skill position player, if you were on the field, the 49ers coach might scheme a play up for you.

In third quarter, he dialed up two passes for fullback Kyle Juszczyk, who had caught a touchdown pass in the first half. And on a key third-and-8, Garoppolo found third receiver Kendrick Bourne for a 26-yard pass to the Chiefs 11.

The 49ers scored three plays later when running back Raheem Mostert, who had once been released by five teams over a span of 15 months — the Eagles the first — powered into the end zone. San Francisco had scored the first 10 points of the second half and were up, 20-10.

Mahomes, meanwhile, was careless. He nearly fumbled when he was stripped-sacked by defensive end Nick Bosa. And a play later he threw an ill-advised third-down pass to Hill that was intercepted by linebacker Fred Warner.

A possession later, Mahomes turned the ball over again when he threw behind Hill on another third down and the ball deflected to 49ers safety Moore.

The quarterback was more workmanlike in the first half, but threw the ball downfield only once — a 28-yard jump ball to receiver Sammy Watkins. The 49ers’ shell zone made sure of that.

Reid called throws on early downs, per usual, and ran in short yardage situations to effect. And he was aggressive on fourth down, converting two. But he had a few dubious calls late before the break, especially a second-down Mecole Hardman end-around that resulted in a 6-yard loss.

The 49ers were more balanced but used their potent ground attack to set up play-action.

The Chiefs started slow, and for the third straight game went three and out on their first possession. The 49ers responded with a 38-yard field goal, their longest play on the drive a 32-yard Deebo Samuel end-around.

The multifaceted receiver was San Francisco’s best offensive weapon in the first 30 minutes. He caught four passes for 23 yards and added another 7-yard carry when he kept and ran for a first down when an option to throw back to Garoppolo was covered by the Chiefs.

By the third quarter, Samuel had already set the Super Bowl record for rushing yards by a receiver.

Kansas City didn’t allow its early deficit to grow any larger, though. Mahomes drove the Chiefs inside the 49ers’ red zone but faced a third-and-11. He scrambled past the marker, but a Jimmy Ward bolt knocked the ball backward out of the quarterback’s hand a yard short.

Reid ran his field goal unit onto the field, but on second thought he gambled. The Chiefs’ pre-snap motion included Temptations-like spin moves that led to a direct snap to Williams. He motored ahead 4 yards down to the 1-yard line. Two plays later, Mahomes kept the ball on reverse speed option for the touchdown.

The 49ers uncharacteristically threw downfield on the first two plays of their next possession, but Garoppolo was hit by defensive tackle Mike Pennel as he threw on the second. The ball fluttered and cornerback Bashaud Breeland hauled in the interception before running out of the bounds at the Chiefs 44.

San Francisco held Kansas City to only a 31-yard field goal, and on its ensuing drive, went back to the ground. Four of the 49ers’ seven plays were rushes for 38 yards. Garoppolo, though, capped the series with a 15-yard touchdown pass to full back Juszczyk on a naked bootleg to knot the score, 10-10.