“We can win a shootout,” Crabtree said. “Whatever it takes, that’s our motto. … We feel like we can do anything, sky’s the limit.”

The 49ers (10-3-1) own at least a wild-card spot and play at Seattle next week with a chance to win the NFC West. A loss would bring the division race down to the final weekend.

Kaepernick threw for four touchdowns, two to Crabtree, who had 107 yards receiving. The defense rattled Tom Brady at times, but also yielded 443 yards passing in a sloppy contest between two of the league’s more precise teams.

AFC East champion New England (10-4), which had won seven in a row, trailed 31-3 in the third quarter and lost for the first time at home in December in 21 games. The Patriots also had won 21 in a row in the second half of the schedule before San Francisco somehow regrouped late in a game it seemingly had clinched long before.

San Francisco forced four turnovers, matching the number of giveaways New England had at home all season.

But then the Niners began sleepwalking, and back came Brady and the Patriots on a 6-yard TD run by Danny Woodhead and a 1-yard dive by Brady. A 5-yard pass to Aaron Hernandez and Woodhead’s 1-yard run with 12:13 remaining tied it.

And just like that, San Francisco went in front again.

Rookie LaMichael James broke free for a 62-yard kickoff return. On the next snap — the third time the Niners would have a one-play TD drive — Crabtree took a pass on the left side, spun and headed into the end zone.

“We faced adversity,” James said. “Nobody flinched.”

New England turned over the ball on downs and Akers made his kick. Stephen Gostkowski added a 41-yarder for the Patriots with 38 seconds remaining, but they couldn’t recover the onside kick.

San Francisco led 17-3 at the half. And they looked safe after Frank Gore picked up Kaepernick’s third fumble and scored on a 9-yard run, followed by Crabtree’s 27-yard score in a pinpoint pass from the second-year quarterback.

The defense set up both of San Francisco’s TDs in the third.

Dashon Goldson returned Steven Ridley’s fumble 66 yards to the New England 3 before Gore found the end zone. Defensive end Aldon Smith, known for his sacks, grabbed a pass out of Hernandez’s hands for his first career interception. After he was tackled, Smith ran directly to the sideline and sat down on the 49ers’ bench.