The San Francisco 49ers produced a stunning second-half fightback to beat the Detroit Lions 34-31 and reach their second Super Bowl in five years on Sunday.

The 49ers set up a rematch of the 2020 Super Bowl against Kansas City after recovering from a 24-7 halftime deficit to score 27 points and seal a dramatic NFC Championship victory in Santa Clara.

The loss was a heartbreaking blow for Detroit, who had been bidding to remove themselves from the list of four NFL teams who have never reached a Super Bowl.

Ultimately, however, Detroit’s risky calls to pass up two kickable field goals in the second half in favor of failed fourth down conversions came back to haunt them.

Instead it was San Francisco who showed the composure that will send them into the February 11 Super Bowl showdown against the Chiefs in Las Vegas, where they will be looking to avenge their 31-20 loss to Kansas City in the 2020 showpiece in Miami.

“We played as bad of a first half as we could but we were still within 17,” San Francisco head coach Kyle Shanahan said.

“It’s been a long year to get to this point and we got it done today. It was hard at the beginning, but the character we have on our team, we can’t wait to get to Vegas.”

Detroit got off to a dream start, with Jameson Williams weaving over from 42 yards on the Lions’ first possession to rock an expectant 49ers crowd, with Michael Badgley adding the extra point.

The 49ers had no answer to the Detroit running game, and the Lions went 14-0 ahead with another rushing touchdown, this time David Montgomery barreling over from a yard out to cap an 11-play, 62-yard drive.

The 49ers finally got on the board early in the second quarter with the first touchdown of the day from Christian McCaffrey, the star running back walking in from two yards out to make it 14-7.

But the San Francisco score was only a fleeting interlude to Detroit’s dominance.

Soon afterwards, Brock Purdy’s deep pass intended for Deebo Samuel was picked off by Malcolm Rodriguez to leave Detroit in prime field position.

The Lions duly took advantage of the interception, with running back Jahmyr Gibbs going over from 15 yards out to help stretch the score to 21-7.

A 21-yard Badgley field goal completed the first half scoring to leave Detroit in total command at 24-7.

But if everything went right for Detroit in the first half, everything went wrong after the restart.

Jake Moody’s 43-yard field goal cut Detroit’s lead to 14 points.

The Lions then made a fateful decision to try a fourth down conversion instead of attempting a field goal on their next possession. The attempt failed, and San Francisco took over.

Fortune then smiled on the 49ers, with Purdy’s 51-yard bomb to Brandon Aiyuk bouncing off the faceguard of cornerback Kindle Vildor into the arms of Aiyuk.

Momentum shift

Three plays later, Purdy hit Aiyuk in the end zone for a touchdown to leave the 49ers just one score away from tying the game.

Once again, Lady Luck intervened, with Gibbs fumbling on the first play of the Lions’ next possession after the restart.

San Francisco quarterback Purdy was soon scrambling 21 yards to the Detroit four-yard line, and McCaffrey then finished the drive with another close-range touchdown to help tie the score.

The momentum shift continued with the Niners taking the lead with a Moody field goal that made it 27-24.

Another failed Detroit fourth down attempt — from kickable field goal range — then handed possession back to San Francisco.

The Niners went 70 yards upfield for McCaffrey to score and make it 34-24.

Detroit got a late touchdown through Williams to get within three, but it was too little too late as San Francisco regained possession to secure the win.

Shattered Lions head coach Dan Campbell later defended his decision to spurn the second-half field goal opportunities.

“I just felt really good about us converting, and getting our momentum back,” Campbell said. “It’s easy hindsight, and I get it. But I don’t regret those decisions.

“We had an opportunity and we just couldn’t close it out,” he added. “It stings…it’s like getting your heart ripped out.”