FOXBOROUGH — The Patriots clawed back from a four-touchdown hole against the 49ers on a cold and rainy Sunday night, but they could not complete their comeback.

With white knuckles and clenched teeth, San Francisco clung to its lead as the Patriots posted score after score in the second half. Though the Niners never relinquished the lead, the Patriots tied the score with 6:43 left and seemed to have momentum fully on its side.

But in one play — a 38-yard Colin Kaepernick to Michael Crabtree catch-and-run — San Francisco reclaimed the lead. This time its defense held firm, and the 49ers won, 41-34.

Advertisement - Continue Reading Below

They were able to leave Gillette Stadium as the first visiting team to post a December win here since the Jets on Dec. 22, 2002, a span of 21 games.

“Obviously, we didn’t do anything well enough to win,” said Patriots coach Bill Belichick. “It starts with me. It wasn’t good enough on offense, it wasn’t good enough on defense, it wasn’t good enough on special teams. We have to perform better than that. We just didn’t do a good job tonight. Simple as that.”

“I had a feeling we’d be able to come back, we hung in there, but we made too many mistakes,” Tom Brady said. “It’s a tough loss. We’ve got some things to correct.”

On the winning touchdown, Kaepernick threw a quick pass to Crabtree, who ran up the left sideline and got the ball just over the goal line.

With a little more than six minutes still on the clock, and given the way the Patriots were playing on offense, it was feasible that San Francisco had left too much time on the clock for Brady.

But Brady, who set a career high with 65 pass attempts, completing 36 for 443 yards, was sacked on back-to-back plays on the Patriots’ possession following that touchdown, and the offense turned the ball over on downs on its next possession.

Brady was sacked three times, and the 49ers were credited with seven quarterback hits.

“They just played better,” said Patriots center Ryan Wendell. “They executed better. I can’t speak to the other phases of the game, but we didn’t do a good job protecting Tom, we didn’t do a good job running the ball.”

San Francisco sealed the win with a 28-yard field goal at the two-minute warning.

For as poorly as the Patriots played for two-plus quarters — they had more first-half punts (4) than points (3) — the game followed a now-familiar pattern: a team gets golden opportunities to take a lead, or build a lead, or put a game away against the Patriots, slip away.

And so it was for the 49ers.

With no fewer than three first-half chances to extend an early lead thanks to turnovers and excellent field position, the Patriots were lucky to go into halftime only down, 17-3, and if San Francisco wasn’t thinking it in the locker room, before the night was done it had to be thinking back on all of the points left on the board in the opening 30 minutes.

One of Belichick’s tenets of winning football is being on the positive side when it comes to turnovers, and that was one of the big issues for New England: Though the 49ers fumbled six times, they recovered all but one, and Kaepernick threw one goal-line interception (another missed opportunity). However, New England lost two fumbles, one each from Stevan Ridley and Shane Vereen, and Brady was intercepted twice.

“It came down to execution,” Brady said. “There’s no magic formula. We were killing ourselves.”

New England started with the ball after San Francisco won the toss and deferred, and on his first carry Ridley fumbled, and it was recovered by the 49ers. But since all change-of-possession plays are reviewed, referee Ed Hochuli (he of the large biceps and long-winded explanations) declared that Ridley was down before the ball came out.

On third and 1, it looked like Brady was going to run a sneak, but handed off to Ridley, who was dropped for a 2-yard loss.

That was the first of seven failed third-down attempts for New England, which went 0 for the half in that category. On the night, the Patriots converted just 2 of 15. They entered the game converting 52.5 percent of their third-down chances, by far the best in the league.

Randy Moss, who started with Mario Manningham unable to play because of a shoulder injury, scored the first touchdown of the game, on the 49ers’ first possession.

After gains of 11 and 13 yards to Michael Crabtree and then a 12-yard pass to Moss, the 49ers were well inside New England territory. Third and long from the 24 saw Kaepernick looking to Moss again.

The veteran left Patriots rookie cornerback Alfonzo Dennard in the dust, safety help from Devin McCourty came too late, and Moss had his third touchdown of the season.

Brady completed a 23-yard pass to Brandon Lloyd to start New England’s next drive, but then was drilled in the back by Aldon Smith. Two plays later, again on third down, Smith got a free run at Brady, who threw the ball into the turf.

Possession No. 3 ended quickly, with Brady’s fifth interception of the season.

He looked deep and Wes Welker and Carlos Rogers got tangled up as they were tracking the pass. Perhaps because of that, the ball was out in front of Welker, and Rogers got the interception. The cornerback ran it all the way back to the 5, where Brady tripped him up.

But despite being so close to paydirt, the 49ers came away emptyhanded: first down was a pass out of the end zone, and on second down, Kaepernick completed a short pass to tight end Delanie Walker. Hit by Steve Gregory, Walker fumbled, and Dennard recovered.

New England did nothing and went three-and-out.

On the 49ers’ next possession, coach Jim Harbaugh made a gutsy call on fourth and 10, having the ball snapped to defensive back Dashon Goldson instead of punter Andy Lee. Goldson made it to the New England 28, a gain of 31 yards.

The second chance, however, ended with a missed 39-yard field goal attempt from David Akers.

Another Patriots lost fumble, this one from Vereen, again netted no points for the Niners.

New England got on the board in the second quarter with a Stephen Gostkowski field goal, but needed 16 plays to cover 62 yards.

San Francisco scored 10 more points before halftime, on a 34-yard Kaepernick-to-Walker touchdown, again with Dennard as the nearest defender, though his reaction suggested he was expecting safety help. There was also the field goal from Akers to end the half.

Though Brady found some solace in the way his team battled back in the second half, as Welker noted, it’s still a loss.

“We started playing better football and started really trying to put the plays together and move the ball down the field. It was just too late,” Welker said.

“It’s very disappointing,” added Jerod Mayo. “When you lose a game it’s always disappointing, but especially to lose like this. We prepared well, we couldn’t get a turnover, we’ve been getting turnovers.”