The 49ers had taken a three-point lead on a 16-yard touchdown pass from Troy Smith to Michael Crabtree with 2:10 left in the fourth quarter, but Rams kicker Josh Brown forced an overtime when he hit a 33-yard field goal as time expired.
Smith tossed for a career-best 356 yards and a touchdown, going 17-of-28, and Frank Gore ran for 87 yards and a score on 22 carries for San Francisco (3-6).
The Rams, meanwhile, lost for the second time in three games.
Sam Bradford finished the game with 251 yards and one touchdown, and Steven Jackson gained 81 yards on 20 carries with a score for St. Louis (4-5).
Pistorius testifies he didn't consciously pull trigger when he shot girlfriend
'Happy' fan videos make Pharrell cry [VIDEO]