FOXBOROUGH, Mass. -- Jim Harbaugh might have learned more about his San Francisco 49ers during 20 minutes of sleepwalking than at any other time in his two years as their coach.

His 49ers turned a nearly historic collapse into a stunning victory and a playoff berth. They withstood a 28-point comeback by the New England Patriots to win 41-34 on Sunday night in the rain.

"Our team has now hung in in a lot of big-time pressure games," Harbaugh said. "They've overcome adversity. They've shown they can do that."

Michael Crabtree took a short pass from Colin Kaepernick and sped around cornerback Kyle Arrington for a 38-yard touchdown with 6:25 to go, then David Akers made a 28-yard field goal to clinch it.

"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.

"I used to live next to a train station in Chicago," Harbaugh said. "And it's like the more you hear the train, the less you hear it. I feel that way with our team in terms of pressure in big games. The more you feel it, the less you feel it."

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

But then the Niners began collapsing, 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 6:43 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.

He was back up on his feet cheering the next play, when Crabtree broke free to make it 31-3.

"We just spotted them 28 points," Brady said. "We fought hard, but you can't play poorly against a good team and expect to win. We can't miss plays that we have opportunities with."

Still, no one can relax against the Patriots.

Unlike a week ago, when the Patriots routed Houston, they fell behind quickly in the rain and ran only 10 snaps on their opening three series. San Francisco's fearsome pass rush was sharp then, and Brady was hit on the arm twice while trying to pass.

Even worse, his long throw on their third possession for Wes Welker was picked off by Carlos Rogers, who then slalomed his way on the wet turf toward the New England end zone. Only Brady stood in his way at the 5, and Rogers fell trying to elude him.

It was a key stop because Delanie Walker fumbled two plays later.

Earlier, Kaepernick accounted for 60 yards through the air on the 49ers' first drive. Randy Moss showed the kind of elusiveness that made him a record-setter in New England from 2007 until he was traded early in the 2010 by getting behind the secondary for a 24-yard TD.

His short celebration as he faced the crowd drew loud hoots.

Brady preventing Rogers from scoring was about the only highlight for the Patriots in the opening quarter, but the 49ers weren't any more effective beyond their scoring drive and a 38-yard run by Goldson on a fake punt. The slopfest included Akers' being wide left on a 39-yard field goal.

All this from teams ranked 1-2 in fewest giveaways.

"We just didn't even give ourselves a chance," Brady said.

When the Patriots finally got their usually unstoppable offense going, they used 16 plays and converted a fourth down. But they stumbled inside the 10 when Brady was sacked by Ray McDonald. Gostkowski made a 32-yard field goal.

San Francisco answered quickly, helped by a 35-yard pass interference call on Aqib Talib. Walker slipped behind a zone defense for a 34-yard TD pass from Kaepernick, making it 14-3.

Akers made a 20-yard field goal as the half ended, finishing a 15-play, 76-yard drive. The three points were the Patriots' fewest in a half all season, and they were outgained 249-113.

Of course, that turned around in the second half.

Aside from the players' mistakes, the game also was slowed by officiating confusion that led to several lengthy conferences. One delay took about 10 minutes to decide whether 49ers punt returner Ted Ginn Jr., muffed a second-quarter kick.

NOTES: Welker now has 100 catches this season, the fifth time he has reached that number, an NFL record. ... New England has 506 points, the fourth time it has reached 500, also a league mark. ... San Francisco had allowed only 184 points going into the game, lowest in the league. ... Andy Lee averaged 54 yards net on five punts for the 49ers. ... Brady's 65 throws are a career high. ... Brandon Lloyd had 10 catches for 190 yards for New England.

___

Loading Slideshow...
  • Colin Kaepernick, Randy Moss

    San Francisco 49ers wide receiver Randy Moss (84) celebrates his touchdown catch from quarterback Colin Kaepernick (7) in the first quarter of an NFL football game against the New England Patriots in Foxborough, Mass., Sunday, Dec. 16, 2012. (AP Photo/Steven Senne)

  • DeMarco Murray, Dez Bryant

    Dallas Cowboys running back DeMarco Murray (29) celebrates his touchdown against the Pittsburgh Steelers with Dez Bryant (88) during the second half of an NFL football game Sunday, Dec. 16, 2012 in Arlington, Texas. (AP Photo/Tony Gutierrez)

  • Patrick Willis, Donte Whitner, Dashon Goldson, Aaron Hernandez

    San Francisco 49ers safeties Donte Whitner (31) and Dashon Goldson, right rear, react in the third quarter of an NFL football game as New England Patriots tight end Aaron Hernandez (81) lies on the turf in Foxborough, Mass., Sunday, Dec. 16, 2012. At left is 49ers inside linebacker Patrick Willis (52). (AP Photo/Elise Amendola)

  • Greg Hardy, Sione Fua

    Carolina Panthers defensive end Greg Hardy (76) celebrates after sacking San Diego Chargers quarterback Philip Rivers as teammate Sione Fua, left, looks on during the second half of an NFL football game Sunday, Dec. 16, 2012, in San Diego. (AP Photo/Gregory Bull)

  • Matthew Stafford, Sam Acho

    Detroit Lions' Matthew Stafford (9) gets off a pass as Arizona Cardinals' Sam Acho (94) applies pressure during the second half in an NFL football game on Sunday Dec. 16, 2012, in Glendale, Ariz. The Cardinals defeated the Lions 38-10. (AP Photo/Ross D. Franklin)

  • Mike Tomlin

    Pittsburgh Steelers head coach Mike Tomlin walks the sideline during the second half of an NFL football game against the Dallas Cowboys Sunday, Dec. 16, 2012 in Arlington, Texas. (AP Photo/LM Otero)

  • Kirk Cousins, Leonard Hankerson

    Washington Redskins quarterback Kirk Cousins (12) congratulates wide receiver Leonard Hankerson after a 54-yard touchdown pass in the first quarter of an NFL football game against the Cleveland Browns in Cleveland, Sunday, Dec. 16, 2012. (AP Photo/Rick Osentoski)

  • Deion Branch

    San Francisco 49ers cornerback Tarell Brown, right, breaks up a pass in the end zone intended for New England Patriots wide receiver Deion Branch (84) in the second quarter of an NFL football game in Foxborough, Mass., Sunday, Dec. 16, 2012. (AP Photo/Elise Amendola)

  • Knowshon Moreno

    Denver Broncos running back Knowshon Moreno leaps into the air to celebrate his touchdown during the second half of an NFL football game against the Baltimore Ravens in Baltimore, Sunday, Dec. 16, 2012. (AP Photo/Nick Wass)

  • Kirk Cousins

    Washington Redskins quarterback Kirk Cousins looks for an open receiver in the third quarter of an NFL football game against the Cleveland Browns, Sunday, Dec. 16, 2012, in Cleveland. (AP Photo/Tony Dejak)

  • Tony Gonzalez, Julio Jones

    Atlanta Falcons tight end Tony Gonzalez (88) celebrates with wide receiver Julio Jones (11) after Jones scored a touchdown during the second half of an NFL football game against the New York Giants, Sunday, Dec. 16, 2012, in Atlanta. (AP Photo/Rich Addicks)

  • San Francisco 49ers fans celebrate their team's 41-34 win over the New England Patriots in an NFL football game in Foxborough, Mass., Monday, Dec. 17, 2012. (AP Photo/Elise Amendola)

  • Jamarca Sanford, Steven Jackson

    St. Louis Rams running back Steven Jackson, left, runs for an 8-yard gain as Minnesota Vikings strong safety Jamarca Sanford gives chase during the third quarter of an NFL football game Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Seth Perlman)

  • Greg Toler, Kerry Rhodes

    Arizona Cardinals' Greg Toler (28) returns an interception for a touchdown against the Detroit Lions as teammate Kerry Rhodes (25) follows during the second half in an NFL football game on Sunday Dec. 16, 2012, in Glendale, Ariz. The Cardinals defeated the Lions 38-10. (AP Photo/Ross D. Franklin)

  • Aaron Hernandez

    A sticker showing a black ribbon to honor the victims of the Sandy Hook Elementary School shootings in Newtown, Conn., is affixed to the helmet of New England Patriots tight end Aaron Hernandez before an NFL football game against the San Francisco 49ers in Foxborough, Mass., Sunday, Dec. 16, 2012. (AP Photo/Elise Amendola)

  • Julio Jones

    Atlanta Falcons wide receiver Julio Jones (11) makes a touchdown catch against New York Giants cornerback Jayron Hosley (28) during the second half of an NFL football game on Sunday, Dec. 16, 2012, in Atlanta. (AP Photo/Rich Addicks)

  • Eli Manning

    New York Giants quarterback Eli Manning (10) sits on the bench during the second half of an NFL football game against the Atlanta Falcons, Sunday, Dec. 16, 2012, in Atlanta. (AP Photo/John Amis)

  • Joe Flacco

    Baltimore Ravens quarterback Joe Flacco walks off the field with his head down during the second half of an NFL football game against the Denver Broncos in Baltimore, Sunday, Dec. 16, 2012. (AP Photo/Patrick Semansky)

  • Evan Royster

    Washington Redskins running back Evan Royster (22) breaks away on a 4-yard touchdown run against the Cleveland Browns in the fourth quarter of an NFL football game on Sunday, Dec. 16, 2012, in Cleveland. (AP Photo/Tony Dejak)

  • Philip Rivers

    San Diego Chargers quarterback Philip Rivers warms up before an NFL football game against the Carolina Panthers, Sunday, Dec. 16, 2012, in San Diego. (AP Photo/Denis Poroy)

  • Danny Amendola, Brandon Gibson

    St. Louis Rams wide receiver Danny Amendola, right, is congratulated by teammate Brandon Gibson after catching a 6-yard touchdown pass during the fourth quarter of an NFL football game against the Minnesota Vikings Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Seth Perlman)

  • Mark Ingram, E.J. Biggers

    New Orleans Saints running back Mark Ingram (28) carries as Tampa Bay Buccaneers cornerback E.J. Biggers (31) tries to tackle in the first half of an NFL football game at the Mercedes-Benz Superdome in New Orleans, Sunday, Dec. 16, 2012. (AP Photo/Bill Feig)

  • Steven Jackson

    St. Louis Rams running back Steven Jackson runs up the middle for a 9-yard gain during the third quarter of an NFL football game against the Minnesota Vikings Sunday, Dec. 16, 2012, in St. Louis. The gain put Jackson over 10,000 yards in his career. (AP Photo/Tom Gannam)

  • Chris Hope, Victor Cruz (

    Atlanta Falcons strong safety Chris Hope (24) hits New York Giants wide receiver Victor Cruz (80) during the second half of an NFL football game on Sunday, Dec. 16, 2012, in Atlanta. (AP Photo/Rich Addicks)

  • Owen Daniels, Joe Lefeged, Ricardo Mathews

    Houston Texans tight end Owen Daniels, bottom, loses his helmet as he is tackled by a group of Indianapolis Colts, including Joe Lefeged (35) and Ricardo Mathews, top right, in the third quarter of an NFL football game on Sunday, Dec. 16, 2012, in Houston. (AP Photo/Eric Gay)

  • Mike Williams, Jabari Greer

    Tampa Bay Buccaneers wide receiver Mike Williams (19) reaches for a pass in front of New Orleans Saints cornerback Jabari Greer (33) in the first half of an NFL football game at the Mercedes-Benz Superdome in New Orleans, Sunday, Dec. 16, 2012. (AP Photo/Bill Feig)

  • Mark Ingram, Mason Foste

    New Orleans Saints running back Mark Ingram (28) carries as Tampa Bay Buccaneers middle linebacker Mason Foster (59) and strong safety Mark Barron (24) cover in the first half of an NFL football game at the Mercedes-Benz Superdome in New Orleans, Sunday, Dec. 16, 2012. (AP Photo/Bill Haber)

  • Stephen Burton, Janoris Jenkins

    Minnesota Vikings wide receiver Stephen Burton, left, is sent flying by St. Louis Rams cornerback Janoris Jenkins after catching a pass for a 5-yard gain during the third quarter of an NFL football game Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Tom Gannam)

  • Joel Dreessen, Ed Reed

    Denver Broncos tight end Joel Dreessen pulls in a pass under pressure from Baltimore Ravens free safety Ed Reed during the second half of an NFL football game in Baltimore, Sunday, Dec. 16, 2012. (AP Photo/Nick Wass)

  • Knowshon Moreno

    Denver Broncos running back Knowshon Moreno runs in to the end zone for a touchdown during the second half of an NFL football game against the Baltimore Ravens in Baltimore, Sunday, Dec. 16, 2012. (AP Photo/Nick Wass)

  • Vick Ballard, Glover Quin, Darryl Sharpton

    Indianapolis Colts running back Vick Ballard (33) rushes for a gain as Houston Texans' Glover Quin (29) and Darryl Sharpton (51) pursue in the third quarter of an NFL football game on Sunday, Dec. 16, 2012, in Houston. (AP Photo/Dave Einsel)

  • Andrew Luck

    Indianapolis Colts quarterback Andrew Luck (12) reacts after throwing a touchdown pass to Dwayne Allen in the third quarter of an NFL football game against the Houston Texans, Sunday, Dec. 16, 2012, in Houston. (AP Photo/Eric Gay)

  • James Jones, Jermichael Finley

    Green Bay Packers wide receiver James Jones (89) celebrates his touchdown reception with teammate Jermichael Finley (88) in the second half of an NFL football game in Chicago, Sunday, Dec. 16, 2012. (AP Photo/Charles Rex Arbogast)

  • Blair Walsh, Chris Kluwe

    Minnesota Vikings kicker Blair Walsh (3) and teammate Chris Kluwe watch as Walsh's 53-yard field goal is good during the third quarter of an NFL football game against the St. Louis Rams Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Tom Gannam)

  • Leonard Hankerson

    Washington Redskins wide receiver Leonard Hankerson (85) runs free to the end zone on a 2-yard touchdown catch against the Cleveland Browns in the third quarter of an NFL football game on Sunday, Dec. 16, 2012, in Cleveland. (AP Photo/Tony Dejak)

  • Aaron Rodgers, Mike McCarthy

    DELETES REFERENCE TO BEARS WINNING -- Green Bay Packers quarterback Aaron Rodgers (12) talks with head coach Mike McCarthy in the second half of an NFL football game against the Chicago Bears in Chicago. Sunday, Dec. 16, 2012. (AP Photo/Charles Rex Arbogast)

  • Adrian Peterson, Kendall Langford

    Minnesota Vikings running back Adrian Peterson, left, runs for a 13-yard gain as St. Louis Rams defensive tackle Kendall Langford defends during the second quarter of an NFL football game Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Tom Gannam)

  • Reggie Bush, Russell Allen

    Miami Dolphins running back Reggie Bush (22) runs as Jacksonville Jaguars outside linebacker Russell Allen (50) holds on to his leg during the first half of an NFL football game on Sunday, Dec. 16, 2012, in Miami. (AP Photo/John Bazemore)

  • Alfred Morris

    Washington Redskins running back Alfred Morris celebrates after scoring on a 3-yard touchdown run against the Cleveland Browns in the third quarter of an NFL football game in Cleveland, Sunday, Dec. 16, 2012. (AP Photo/Rick Osentoski)

  • Eric Decker, Cary Williams

    Denver Broncos wide receiver Eric Decker pulls in a touchdown pass as Baltimore Ravens cornerback Cary Williams looks on during the second half of an NFL football game in Baltimore, Sunday, Dec. 16, 2012. (AP Photo/Nick Wass)

  • Michael Crabtree

    San Francisco 49ers wide receiver Michael Crabtree (15) spikes the ball over the goal post after scoring a touchdown against the New England Patriots in the third quarter of an NFL football game in Foxborough, Mass., Sunday, Dec. 16, 2012. (AP Photo/Elise Amendola)

  • Randall Cobb, D.J. Moore

    Green Bay Packers wide receiver Randall Cobb (18) is tackled by Chicago Bears cornerback D.J. Moore (30) in the first half of an NFL football game in Chicago, Sunday, Dec. 16, 2012. (AP Photo/Nam Y. Huh)

  • Eli Manning

    New York Giants quarterback Eli Manning (10) speaks to Giants wide receiver Victor Cruz (80) against the Atlanta Falcons during the first half of an NFL football game on Sunday, Dec. 16, 2012, in Atlanta. (AP Photo/John Amis)

  • Bryan Braman, Johnathan Joseph

    Houston Texans linebacker Bryan Braman (50) celebrates with Johnathan Joseph, left, after blocking a punt and returning it for a touchdown in the second quarter of an NFL football game against the Indianapolis Colts, Sunday, Dec. 16, 2012, in Houston. (AP Photo/Eric Gay)

  • Kevin Williams, Sam Bradford

    St. Louis Rams quarterback Sam Bradford, right, and Minnesota Vikings defensive tackle Kevin Williams reach for a ball fumbled by Bradford during the second quarter of an NFL football game Sunday, Dec. 16, 2012, in St. Louis. The Vikings' Chad Greenway recovered the fumble. (AP Photo/Seth Perlman)

  • Lance Moore

    New Orleans Saints wide receiver Lance Moore (16) pulls in a touchdown pass as Tampa Bay Buccaneers free safety Ahmad Black (43) covers in the first half of an NFL football game at the Mercedes-Benz Superdome in New Orleans, Sunday, Dec. 16, 2012. (AP Photo/Bill Feig)

  • Bryan Braman

    Houston Texans linebacker Bryan Braman (50) celebrates on the bench after blocking a punt and returning it for a touchdown in the second quarter of an NFL football game against the Indianapolis Colts, Sunday, Dec. 16, 2012, in Houston. (AP Photo/Dave Einsel)

  • Jared Allen, Everson Griffen

    Minnesota Vikings defensive end Everson Griffen, right, is congratulated by teammate Jared Allen after intercepting a pass and running it back 29 yards for a touchdown during the second quarter of an NFL football game against the St. Louis Rams Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Tom Gannam)

  • Matt Ryan

    Atlanta Falcons quarterback Matt Ryan (2) works during the first half of an NFL football game against New York Giants, Sunday, Dec. 16, 2012, in Atlanta. (AP Photo/John Amis)

  • Adrian Peterson

    Minnesota Vikings running back Adrian Peterson (28) breaks free for an 82-yard touchdown run during the second quarter of an NFL football game against the St. Louis Rams Sunday, Dec. 16, 2012, in St. Louis. (AP Photo/Seth Perlman)

  • Tom Brady

    New England Patriots quarterback Tom Brady (12) walks toward the sideline in the second quarter of an NFL football game against the San Francisco 49ers in Foxborough, Mass., Sunday, Dec. 16, 2012. (AP Photo/Elise Amendola)