Despite reaching the Super Bowl in two of the last three years, the Rams entered the regular-season finale 6-9. Things looked equally bleak as St. Louis trailed, 20-3, following a 44-yard field goal by Jeff Chandler 77 seconds into the fourth quarter.
But Martin rallied the Rams, finding tight ends Brandon Manumaleuna and Ernie Conwell and wide receiver Isaac Bruce for scoring strikes.
The 49ers, who were locked into the fourth playoff spot regardless of the outcome, played their starters sparingly. Jeff Garcia engineered a lengthy scoring drive to open the game, then sat the final three quarters.
San Francisco (10-6) will host the New York Giants on Sunday.