SANTA CLARA, Calif. — Brock Purdy threw for 267 yards and a touchdown, and the San Francisco 49ers rallied from 17 points down at halftime to beat the Detroit Lions 34-31 on Sunday to reach the Super Bowl.

The 49ers (14-5) scored 17 points in an eight-minute span of the third quarter to tie the NFC championship game and then pulled away in the fourth quarter to earn a rematch against Kansas City after losing to the Chiefs in the Super Bowl four years ago.

San Francisco mounted the fourth comeback ever from 17 points down or more in a conference title game thanks to big plays by Purdy and bad mistakes from the Lions (14-6), including two failed fourth downs in field goal range. Detroit fell short of reaching the first Super Bowl in franchise history.

After being questioned about whether he could lead a comeback, Purdy has done it twice in as many weeks. He engineered a game-winning drive in the fourth quarter to beat Green Bay last week and then had an even bigger comeback against the Lions.

The Baltimore Banner thanks its sponsors. Become one.

Christian McCaffrey had two TD runs and little-used backup Elijah Mitchell scored on a 3-yard run to make it 34-24 with 3:02 to play as the Niners got over the conference title game hump after losing the past two seasons.

The Niners blew a 10-point lead in the fourth quarter of the NFC championship game against the Los Angeles Rams two years ago and then were forced to play much of last year’s title game loss at Philadelphia without a functioning quarterback after Purdy injured his elbow on the opening drive and fourth-stringer Josh Johnson left with a concussion early in the third quarter.

But San Francisco managed to make the long journey back to this stage and now is in position to deliver the franchise its record-tying sixth Super Bowl title and first since the 1994 season.

A magical season for the Lions ended in heartbreak. Detroit remains the only team to play every season of the Super Bowl era without reaching the ultimate game. This looked like it could be the year to end that drought when Detroit won back-to-back playoff games after winning just one in the previous 56 seasons.

But the Lions couldn’t finish the job despite holding a 24-7 halftime lead.

The Baltimore Banner thanks its sponsors. Become one.

San Francisco settled for a field goal on the opening drive of the second half before the game completely flipped in a four-minute span.

Detroit coach Dan Campbell opted to go for it on fourth-and-2 from the San Francisco 28, but Josh Reynolds couldn’t hold onto a pass from Jared Goff, leading to a turnover on downs.

The Lions then seemed to have a potential interception when Purdy’s deep pass hit Kindle Vildor in the face mask, but the ball popped up and was caught for a spectacular 51-yard gain by Brandon Aiyuk. Purdy found Aiyuk three plays later for a 6-yard score.

Jahmyr Gibbs then fumbled on the next play from scrimmage, setting up a 1-yard run by McCaffrey to tie the game at 24.

It only got worse from there for the Lions with Reynolds dropping another pass on third down, leading to a punt that Detroit had a chance to down at the 1 but botched.

The Baltimore Banner thanks its sponsors. Become one.

Purdy then drove the Niners to a 33-yard field goal by Jake Moody and their first lead of the game.

Campbell bypassed a chance at a game-tying field goal on fourth-and-3 from the Niners’ 30 midway through the fourth quarter. Goff threw an incomplete pass, and the Niners drove to the insurance touchdown.

Goff added a TD pass to Jameson Williams with 56 seconds to play, but the 49ers recovered the onside kick to seal the win. Goff finished 25-for-41 for 273 yards and a touchdown.

San Francisco’s heralded front seven had no answer in the first half for Detroit’s offensive line, which repeatedly opened up big holes, giving the backs several yards even before first contact.

The Lions ran for 148 yards in the first half, getting TD runs from Williams, David Montgomery and Gibbs.

Michael Badgley added a late field goal to give Detroit a 24-7 lead at the half, tied for the second-biggest scoring output for a road team in the first half of a conference title game since the NFL-AFL merger.