MIAMI GARDENS, Fla. — The Kansas City Chiefs waited a half-century between Super Bowl appearances. Then they waited more than three quarters Sunday evening at Hard Rock Stadium for quarterback Patrick Mahomes and their dynamic offense to get revved up.

All that waiting was worth it. Mahomes delivered a pair of touchdown passes as part of a 21-point outburst as the Chiefs came back to beat the San Francisco 49ers, 31-20, with a stunning late turnaround in Super Bowl LIV on a picture-perfect South Florida night.

“I just went out there and kept competing,” said Mahomes, who was named the game’s MVP. “I knew we weren’t in an ideal situation. But I believed in my defense to get stops. They did. And the guys kept believing in me to start making plays down the field, and we found a way to win.”

The Chiefs surrendered the first 10 points of the second half and trailed 20-10 midway through the fourth quarter. But Mahomes, who had thrown a pair of earlier interceptions, delivered a one-yard touchdown pass to tight end Travis Kelce with just more than six minutes remaining. The Chiefs got the ball back, and Mahomes threw a five-yard touchdown pass to running back Damien Williams with 2:44 left.

After the Kansas City defense sacked 49ers quarterback Jimmy Garoppolo on a fourth-down play, Williams added a 38-yard touchdown run with 1:12 to play to put the Chiefs and their fans in full celebration mode. The Chiefs secured the second Super Bowl triumph in franchise history. They beat the Minnesota Vikings in Super Bowl IV in January 1970 in their most recent appearance.

“We never are out of a game,” Chiefs linebacker Damien Wilson said. “Never. … It’s not how you start. It’s how you finish. We’re the embodiment of that.”

The Chiefs overcame deficits in their AFC playoff victories over the Houston Texans and Tennessee Titans. Their latest comeback denied the 49ers what would have been their sixth Super Bowl win.

“That team, that’s kind of how they’ve been all year,” 49ers Coach Kyle Shanahan said of the Chiefs. “They’re not a team that does it every drive. They get a little bit hot and cold. They can score very fast.”

Garoppolo had a touchdown throw to fullback Kyle Juszczyk in a 20-for-31, 219-yard passing performance for the 49ers. He threw two interceptions, the second of which came in the game’s final minute. Tailback Raheem Mostert had a rushing touchdown for San Francisco.

“It hurts everybody in our room, losing a Super Bowl,” Shanahan said. “We had an opportunity to win that and came up short. But win or lose, it doesn’t change how I feel about our team. … They were better than us today. We can deal with that. But we’re obviously pretty disappointed.”

Mahomes became a Super Bowl winner in his third NFL season. He had a first-half rushing touchdown and ended up completing 26 of 42 passes for 286 yards. Chiefs Coach Andy Reid added a Super Bowl victory to his distinguished career.

“He’s one of the greatest coaches of all time,” Mahomes said. “I don’t think he needed a Lombardi Trophy to prove that. But just to do that, it puts all doubt aside. He’s going to be listed as one of the all-time great coaches in history whenever he wants to be done, which I hope is not anytime soon.”

Reid had lost his previous Super Bowl head coaching appearance while with the Philadelphia Eagles. He seemed on his way to another Super Bowl defeat until Mahomes connected with wide receiver Tyreek Hill for a 44-yard gain on a third-and-15 play to set up the first of the Chiefs’ three late touchdowns. From there, their offense appeared typically unstoppable.

“I told them that I’d coach another 20 years if I could have that group right there, man,” Reid said. “They’re a beautiful bunch — resilient, tough, tough-minded. … I’m so proud of them.”

Reid did his part by sending his offense back on the field for a fourth-and-one try from the San Francisco 5-yard line in the opening quarter, trailing by a field goal. As the Chiefs lined up, Mahomes and three other players performed a synchronized twirl to their right as part of a shift. Williams took a direct snap and ran for four yards and a first down at the 1. Mahomes scored from there on a second-down quarterback keeper.

Garoppolo threw an interception to hand the Chiefs a second-quarter field goal. But Garoppolo and the 49ers regrouped quickly. A superb drive ended with Garoppolo connecting with Juszczyk for a 15-yard touchdown. The 49ers had a chance to take the lead just before halftime. But Shanahan at first took a conservative approach to clock management. When the 49ers suddenly turned aggressive, a long completion from Garoppolo to tight end George Kittle was negated by an offensive pass interference penalty called against Kittle for a light push-off.

“The ref made the call,” Kittle said, “and I’ve got to live with it.”

Shanahan opted against a fourth-and-two try from the Kansas City 24-yard line in favor of a field goal and a 13-10 lead in the third quarter. Mahomes threw an interception that led to Mostert’s one-yard touchdown run that gave the 49ers a 20-10 lead.

“I just tried to fight,” Mahomes said. “I thought that was the biggest thing. Obviously the third quarter didn’t go the way I wanted it to. I tried to force some things and got some turnovers. That’s a really good [49ers] defense. … I didn’t play to my liking in the third quarter. The guys believed in me and gave me confidence and kept fighting, and we found ways to win at the end.”

Mahomes and the Chiefs just were getting started, as it turned out.

“He’s greatness in a bottle,” Wilson said. “He showed up big for us today. Everybody did.”