ST. LOUIS - The San Francisco 49ers defence got beat early by unfamiliar plays and Austin Davis calling the signals.
"No disrespect, but I didn't know who their quarterback was," linebacker Ahmad Brooks said. "We got some good pressure on him. That helped us."
Davis was sacked five times, twice each by Brooks and Dan Skuta. They carried the day, keeping the St. Louis Rams offence stalled, and giving Colin Kaepernick a chance to warm up in a 31-17 victory on Monday night.
Kaepernick threw for 343 yards and three touchdowns for the 49ers, who erased an early 14-point deficit. The defence stepped up again when Dontae Johnson returned an interception return for a TD in the final minute to seal it.
"Talk about an opportunity, I had the chance to make a play," Johnson said.
Brandon Lloyd, Anquan Boldin and Michael Crabtree each caught a scoring pass for the 49ers (4-2), who have won three in a row. The 49ers permitted one first down on St. Louis' first six possessions of the second half.
"We've had the ball in every game with a chance to either win or tie," Davis said. "That's where hopefully my experience will help, the more opportunities I get.
"You've got to be able to go win games at the end."
Kaepernick had plenty of time to throw and wasn't sacked.
"We just want to win," Kaepernick said. "Whatever it takes, if we're up with the lead or if we have to come from behind."
Kaepernick found Boldin zipping across the end zone for an 11-yard touchdown pass, capping a go-ahead 80-yard drive to start the second half. As the Niners were taking the 17-14 lead, several fans in the upper deck unfurled a large banner above the videoboard on the opposite end of the stadium as part of ongoing protests about the Michael Brown shooting in suburban Ferguson in August.
San Francisco then added to its advantage when Kaepernick connected with Crabtree for a 32-yard score with 13 seconds left in the third.
The Rams had one last chance to tie the game, but Johnson returned an interception 20 yards for the clinching score with 53 seconds to go.
It was a disappointing conclusion for St. Louis, which had 151 yards and a 14-0 lead after the first quarter on Benny Cunningham's 1-yard run and a 22-yard touchdown reception for Lance Kendricks.
"We always see different looks in a game," Skuta said. "They hadn't done some of the things they did early. We adjusted and we kept playing, that's something we do really well."
St. Louis wore throwback uniforms — royal blue and bright yellow — to honour its 1999 Super Bowl title team and represented that winning squad very well for a short time. Guard Adam Timmerman joined wide receivers Isaac Bruce and Torry Holt for an impromptu bob n' weave TD dance in the end zone at halftime.
The Rams haven't had a winning season since the Greatest Show on Turf petered out in 2003. They doubled their first-quarter scoring for the year with a start that mimicked their last home game when they led Dallas 21-0.
They lost that one 34-31, and this one was all San Francisco the rest of the way.
The 49ers scuffled to a 1-2 start coming off a third straight appearance in the NFC championship game, but have won 13 of their last 14 in October.
Davis was 21 of 42 for 236 yards. He threw for 300 yards each of the last two games with six touchdown passes.
Three of Davis' four interceptions have been returned for touchdowns, all in the fourth quarter at home.
The 49ers had four penalties before the end of their first possession, three accepted, and one the rest of the way. One of the early miscues, illegal contact on Eric Reid, handed the Rams a second first-and-goal on their opening drive capped by Cunningham's TD behind extra lineman Tim Barnes.
Rookie Tre Mason's first career carry was a 24-yarder that might have gone the distance had he not run into teammate Brian Quick. Three plays later, Kendricks got well behind the defence on a 22-yard catch that made it 14-0 late in the first.
The 49ers inched back on Phil Dawson's 54-yard field goal, his 10th in a row with four beyond midfield. They were backed up at their own 5 inside the two-minute warning but declined to play it safe and Lloyd whipped Janoris Jenkins in single coverage on an 80-yard touchdown catch with 14 seconds left in the half.
"We had Brandon Lloyd on a double move," Kaepernick said. "We liked the matchup. He did an amazing job."
AP NFL website: www.pro32.ap.org and www.twitter.com/AP_NFL