FOXBORO, Mass. -- The 49ers' defense made the best offense in the NFL look inept for most of Sunday night before the more familiar version of the New England Patriots showed up.

And when quarterback Tom Brady and the Patriots appeared, they sent the 49ers reeling with an offensive explosion that rocked the 49ers and put a repeat of their NFC West title in serious jeopardy.

The Patriots scrambled back from a 28-point third-quarter deficit only to have the 49ers respond in lightning-quick fashion.

After LaMichael James' 62-yard kickoff return, quarterback Colin Kaepernick teamed up with Michael Crabtree on their second touchdown of the game for a 38-31 lead with 6:25 remaining.

It was a wild 41-34 victory over the Patriots on a cold and drizzly Sunday night at Gillette Stadium in front of a national-television audience.

The 49ers set the tone early on defense, and Kaepernick threw two touchdown passes to enable the 49ers to build a two-touchdown lead at halftime.

The 49ers extended their lead by another two touchdowns early in the third quarter before the Patriots rallied.

As the 49ers built a 28-point lead in the third quarter, the 49ers looked like a legitimate Super Bowl contender. The 49ers (10-3-1) clinched a spot in the NFC playoffs with the victory and needs just one victory in their final two games of the season to wrap up the NFC West title for the second time in Jim Harbaugh's two seasons as head coach.

The 49ers return to action next week at second-place Seattle before closing out the regular season against the Arizona Cardinals.

Kaepernick had a difficult time getting a handle of the ball on snap exchanges with center Jonathan Goodwin, but even that really did not matter. Running back Frank Gore picked up one of those loose balls and scored on a 9-yard fumble recovery.

Gore's scoring play came one play after safety Dashon Goldson returned a Stevan Ridley fumble 66 yards early in the third quarter. Donte Whitner forced the fumble when he stuck his helmet on the football.

A short time after Gore's touchdown, 49ers outside linebacker Aldon Smith intercepted a Tom Brady pass off the hands of tight end Aaron Hernandez, who moments earlier was leveled by Goldson on a deep pass play. Goldson was called for unnecessary roughness for a helmet-to-helmet hit.

On the next play after Smith's interception, Kaepernick drilled a beautiful throw to Michael Crabtree for a 27-yard touchdown to give the 49ers a 31-3 lead.

The Patriots, who fall to 10-4, entered the game as one of the hottest teams in the NFL. New England had won seven consecutive games, including a 42-14 thumping of the Houston Texans on Monday night.

But the 49ers set the tone for the game earlier after winning the toss and electing to defer. The team's defense went onto the field and immediately forced a three-and-out.

The 49ers took over and drove 63 yards on six plays. Kaepernick hit Crabtree on passes of 11 and 13 yards. Randy Moss had a 12-yard reception. Then, Kaepernick and Moss hooked up for a 24-yard touchdown pass just 3 minutes, 55 seconds into the game for a 7-0 lead.

Kaepernick had a lot more difficult handling snaps from Goodwin than he had problems with the Patriots' defense in the first half. Kaepernick fumbled three exchanges, one of which came on a fourth-and-1 situation deep in New England territory early in the second quarter.

The 49ers squandered some scoring chances in the first half, but still built a 17-3 lead after the first 30 minutes of play.

Cornerback Carlos Rogers intercepted a deep Brady pass intended for Wes Welker. Rogers returned it 53 yards before Brady tackled him at the Patriots' 5.

But two plays later, tight end Delanie Walker fumbled after catching a short swing pass from Kaepernick. Safety Steve Gregory forced the fumble and cornerback Aqib Talib recovered.

The 49ers let another scoring chance get away when David Akers missed a 39-yard field-goal attempt to the left. The 49ers got into field-goal range when Goldson, lined up as a fullback on the punt team, took a direct snap and gained 31 yards on a fourth-and-10 situation to the New England 28-yard line.

Akers later hit a 20-yard field goal on the final play of the first half to give the 49ers a 17-3 lead.

Linebacker NaVorro Bowman forced a fumble of former Cal star Shane Vereen late in the first quarter. Cornerback Chris Culliver recovered at the Patriots' 34. But the drive stalled when Kaepernick fell on the ball after mishandling the snap on a fourth-and-1 play.

After the Patriots cut the 49ers' lead to 7-3 early in the second quarter after a 16-play drive, the 49ers struck back quickly.

Talib was called for a 35-yard pass-interference penalty on a deep throw to Crabtree. And Kaepernick hit Walker on a 24-yard touchdown pass against defensive back Alfonzo Dennard.

New England finally got into the end zone after 39 minutes when Danny Woodhead scored on a 6-yard run with 5:59 remaining in the third quarter. Brady drew the Patriots to within two touchdowns on a 1-yard touchdown plunge on a fourth-and-goal play on the first play of the fourth quarter.

The Patriots cut the 49ers' lead to just seven points, 31-24, on Brady's 5-yard touchdown pass to Hernandez with 12:13 remaining in regulation. They came all the way back to tie the game on Woodhead's 1-yard touchdown run in the middle of the fourth quarter.

The Patriots did much of their damage with 49ers defensive tackle Justin Smith on the sideline with a left elbow injury. In his place, Ricky Jean Francois forced a Patriots punt with five minutes remaining after a third-down sack.

GORE SURPASSES 'THE JET': Gore needed 30 yards to become the all-time undisputed 49ers rushing leader. And he got it easily in the first half when he gained 73 yards on his first 15 rushing attempts.

Perry totaled 8,689 yards rushing while playing for the 49ers franchise. However, two seasons while the 49ers were members of the All-America Football Conference do not count in the 49ers' official all-time totals.

Last year, Gore was recognized as the 49ers' all-time rushing leader, surpassing Perry's NFL-only total of 7,344 yards with the 49ers. Perry gained 1,345 yards in the AAFC (1948 and '49).

THIS 'N' THAT: Moss moved into third on the all-time NFL receiving yards list with an impressive opening drive. Moss caught a 24-yard touchdown pass from Kaepernick to give the 49ers a 7-0 lead. Moss ranks behind former 49ers Jerry Rice (22,895) and Terrell Owens (15,934). . . Left guard Mike Iupati (right shoulder) missed the final 12 plays of the first half. He went to the locker room early for observations. He came back out and started the second half. Leonard Davis entered the game in Iupati's place. . . . Outside linebacker Aldon Smith made his presence felt early in the game when the forced Brady into two incompletions with his pressure... The win broke New England's 20-game home win streak in the month of December.