FOXBOROUGH, Mass. -- Somehow, the San Francisco 49ers managed to respond Sunday, and they did it quickly.
Having blown a 31-3, third-quarter lead as the New England Patriots scored 28 points in less than 19 minutes, San Francisco needed just two plays, a kickoff and one play from scrimmage, to retake control en route to a wild 41-34 victory on a cold, rainy night at Gillette Stadium.
After New England's Danny Woodhead tied the game on a 1-yard run with 6:43 left, it took the Niners, who have won four of their last five, 18 seconds to get the winning score.
LaMichael James ran the kickoff back 62 yards to the New England 38-yard line, and San Francisco scored on the next play. Niners quarterback Colin Kaepernick, now 4-1 as a starter, hit Michael Crabtree with a short pass, but Crabtree eluded Kyle Arrington and sprinted into the end zone for his second score of the game.
"LaMichael had that huge return for us, and then Kap and Crabtree got it done," 49ers offensive tackle Joe Staley said. "They had all the momentum, but LaMichael gave us a bump in momentum and great field position."
The win clinched an NFC playoff spot for the Niners (10-3-1) and kept them solidly in first place in the NFC West. The Patriots (10-4) saw their seven-game winning streak end.
The loss also ended New England's 20-game home December winning streak that dated back to 2002, and a 13-game overall December winning streak. It also left the Patriots in the third position for AFC playoff seeding, which would mean having to play on wild-card weekend.
"You can't play poorly against a good team and expect to win," said New England quarterback Tom Brady, who threw a touchdown pass for the 46th consecutive game but was also picked twice while throwing for 443 yards.
After Kaepernick threw three touchdown passes, one to ex-Patriot Randy Moss, to give the visitors a 28-point lead with 10:21 left in the third quarter, Brady brought the Patriots all the way back.
Woodhead ran for two touchdowns, and Brady scored on a fourth-down sneak and threw a 5-yard touchdown pass to Aaron Hernandez.
"I had a feeling we'd be able to come back," Brady said. "We just made too many mistakes when we needed to make the play."
Two penalties on late punts killed the Patriots, who turned the ball over four times to lose the turnover battle for the first time this season. After the second penalty, which pinned them on their 3-yard line, they failed on a fourth-and-1 from their 12, and the 49ers got a 28-yard field goal from David Akers with 1:56 left.
Brady, who was sacked three times and hit or hurried on several other attempts, then took his team down the field. The Patriots elected to take a field goal, a 41-yarder by Stephen Gostkowski, on second down with 38 seconds left. The Patriots then tried an onside kick, but the Niners recovered.
Kaepernick completed 14 of 25 passes for 216 yards, four touchdowns and an interception. Crabtree made seven receptions for 107 yards.
San Francisco's Frank Gore ran 21 times for 83 yards, and he recovered a fumble for a touchdown.
"Kap did a great job," Gore said. "He's a tough quarterback; he kept his poise. He's a football player. He played great."
Brady finished 36-for-65 for 443 yards, one touchdown and two interceptions. New England's Wes Welker, who had five catches for 56 yards, became the first receiver in NFL history with five 100-catch seasons. Brandon Lloyd caught 10 passes for 190 yards, his second big game in a row late in a rough season. Recently re-signed Deion Branch had four receptions for 37 yards.
The loss cost the Patriots a chance at their third straight perfect second half of the season. They had gone 8-0 in the second half of the last two seasons, and were 5-0 this year before the Sunday night defeat.
By all rights, the 49ers should have put the game open early. They made big plays including a 31-yard run on a fake punt by Dashon Goldson and a 53-yard interception return by Carlos Rogers. However, they left all kinds of points on the field. Brady tackled Rogers at the New England 5-yard line before the Niners fumbled the ball right back.
"It says a lot about this team to travel across the country and (win) a late game, and in their environment with cold and rain," Rogers said. "I am pretty sure everyone picked us to lose."
NOTES: Kaepernick fumbled four snaps from center but didn't lose any of them. Gore picked up one and scored from 3 yards out. ... The Patriots wore stickers on their helmets and joined the rest of the NFL with a moment of silence to honor the people killed in Newtown, Conn. One flare was sent into the air for each of the 26 victims. ... Moss shook hands with Patriots owner Robert Kraft on the field before the game. ... The Patriots visit Jacksonville next Sunday, while the 49ers would wrap up the NFC West title if they win at Seattle. ... Confusion over several calls made the game last 3 hours, 40 minutes. ... The teams met for the first time since 2008, and the Niners ended a three-game series losing streak. ... Brady became the fifth quarterback to have five 4,000-yard seasons, and he hit the 30-TD-pass mark for the fourth time. It was his 53rd 300-yard game. ... The Patriots went over 500 points for the season, becoming the first team ever with four 500-point campaigns. ... The wind-chill factor at game time was 29 degrees. ... Welker had been tied with Marvin Harrison and Jerry Rice for the 100-reception record. ... Brady tied a career high with his fourth rushing touchdown of the season. ... Both teams were missing key receivers. Mario Manningham (shoulder) was out for the Niners, who lost Kyle Williams for the season Nov. 25. New England tight end Rob Gronkowski was sidelined due to a broken arm. Gronkowski returned to light practice leading up to the game, but he missed his fourth consecutive game.