SANTA CLARA, Calif. - It all came down to one play.
The 49ers, having allowed a season-high eight sacks, were on the doorstep of the end zone with a chance to take a four-point lead with two seconds remaining to wash away what had been a dreary afternoon.
But as Colin Kaepernick took the snap from rookie center Marcus Martin, making his NFL debut, he bobbled the ball, pushed into the pile for the game-clinching quarterback sneak, and lost the ball in the jumbled mass of bodies.
Rams linebacker James Laurinaitis recovered Kaepernick's fumble in the end zone, stunning what had been a crowd at Levi's Stadium aching for something to get excited about - like a last-second win.
Instead, the 49ers lost 13-10 to a Rams team that came in 2-5, three weeks after San Francisco beat the same team 31-17 on its home field. The 49ers fell to 4-4, their worst record through eight games under Jim Harbaugh since becoming head coach in 2011.
"We got to suck it up," the dejected coach said. "That was a tough loss. We got to play better football, coach better football. Better habits. Play better."
Harbaugh's team came into Sunday off its bye week, following a 42-17 dismantling by the Denver Broncos Oct. 19. In that game, Kaepernick was sacked a season-high six times. Sunday, the Rams sacked Kaepernick six times in the first half alone. They finished with eight, more than doubling their season total of six.
"We got all the talent in the world," left tackle Joe Staley said. "We were doing some dumb stuff and they took advantage of it."
"Dumb blocks. Dumb techniques. Dumb schemes. Played like (expletive)."
The defeat did not come without controversy.
"I know I crossed the line," Kaepernick said of the final play, indicating he thought he scored before fumbling.
But that wasn't the first questionable call made by the officiating crew headed by Jerome Boger.
With 42 seconds left, Kaepernick hit receiver Michael Crabtree toward the front pylon on the right side of the end zone. Crabtree was in the end zone, came back for the ball, and rolled out of bounds.
Inside two minutes the officials reviewed the call to see if the ball broke the plane under Crabtree's control. The ruling that Crabtree made the catch and did not score was upheld.
However, the clock was running, despite Crabtree rolling out of bounds untouched. San Francisco's next snap came with 14 seconds remaining after Boger indicated there would not be a change to game clock following the review. The game clock resumed running potentially taking away 20 seconds for San Francisco to work with.
"They said they would review all of it – the clock, the catch, the spot. They came back with a ‘play stands.’ That was the only explanation,” Harbaugh said.
It was an ugly and disjointed performance by the 49ers' offense. The makeshift offensive line was starting its fifth different combination in eight games, including the NFL debut of Martin, who played just 90 preseason snaps before suffering a dislocated knee cap against the Chargers in the third exhibition game.
"Every week is going to be something different. You just got to learn from your mistakes, watch the film and correct the mistakes," Martin said.
With Martin, 20, in charge of assigning offensive line protections from his position at center, the Rams decided to blitz the 49ers heavily, often bringing more pass rushers than 49ers had blockers.
"They blitzed like a (expletive) ton," Staley said.
San Francisco did well to adjust on the final drive where it looked like they would come away with a field goal, at least, after starting the possession at their own 12-yard line. Stevie Johnson got the drive going with a 25-yard catch up the left side, followed by a 20-yard grab over the middle by Anquan Boldin.
The 49ers were put in position to win the game by a 25-yard pass interference call on cornerback Trumaine Johnson on Crabtree deep down the left sideline.
Frank Gore, who finished with 49 yards on 14 carries, did not get a rushing attempt at the goal line. Instead, the 49ers tried a play-action passing play following the completion to Crabtree that Kaepernick was forced to throw away.
"It don’t get no tougher than this," Gore said. "We didn’t play a good game. Defense gave us the opportunity. Offense didn’t make plays when we had to. The Rams played a good game, they played hard, and they got the win."
The 49ers defense forced two Austin Davis interceptions in the second quarter on back-to-back drives. They were able to capitalize on the first, after struggling offensively following Phil Dawson's 34-yard field goal on the opening possession.
Davis rolled right and was looking for wideout Tavon Austin along the right sideline. But the ball was under-thrown and safety Antoine Bethea was there to make his second interception of the year.
Three plays later, Kaepernick scrambled right after the play broke down and found a wide-open Boldin in the middle of the field for a 27-yard touchdown. Both of Boldin's TD grabs this year have come against St. Louis.
On the Rams' next drive, Davis, again, under-threw Kenny Britt deep down the right sideline and was intercepted by Perrish Cox, who made his fourth pick of the year, good for second in the NFL.
St. Louis kicker Greg Zuerlien hit the decisive 39-yard field goal midway through the fourth after his team was given the ball at San Francisco's 29 following Andy Lee's 23-yard shanked punt out of the end zone.
Unlike the teams' previous meeting, when 49ers receiver Brandon Lloyd scored inside the final minute, it was the Rams that scored before halftime this week. Britt was left alone in the middle of the field after rookie inside linebacker Chris Borland, making his second NFL start in the place of injured Pat Willis (toe), missed a coverage assignment leading to the 21-yard touchdown catch.
The score came three plays after Kaepernick was strip-sacked by Robert Quinn, giving the Rams the ball at the 49ers' 36 just prior to the two-minute warning.
Borland had an outstanding game in Willis' stead, notching a game-high 18 tackles. But it was his one mistake that he said he will remember.
"A loss is a loss. I blew an assignment that cost us a touchdown and may have cost us the game. So I don’t really feel good about it," Borland said.
Kaepernick finished with 237 yards on 22 of 33 completions. Davis completed just 13 of 24 for 105 yards. The 49ers out-gained the Rams 263-193.
The 49ers will pick up the pieces and hope to rebound on the road against the New Orleans Saints (4-4) next week, who won in Carolina Thursday night, giving them three extra days of rest and time to prepare.
San Francisco didn't get any help within the division Sunday, either. The Arizona Cardinals improved to an NFL-best 7-1 with a win 28-17 win over the Cowboys while Seattle (5-3) jumped a game ahead of the 49ers with a 30-24 win over the Raiders.
E-mail Niners Digest: NinersDigest@gmail.com
Follow Editor-in-Chief Chris Biderman on Twitter.