LOS ANGELES — Colin Kaepernick knew how Chip Kelly was thinking.
Everyone on the San Francisco 49ers knew what the first-year head coach was thinking as the offense took the field with 3:10 left in the fourth quarter.
Score a touchdown, go for 2 — and end a 13-game losing streak.
"What else did we have to lose?" cornerback Rashard Robinson said.
Kaepernick threw a 10-yard touchdown pass with 31 seconds remaining and scrambled for a 2-point conversion and the 49ersovercame a 14-point deficit to defeat the Los Angeles Rams 22-21 on Saturday.
Kaepernick rolled to his right while looking for a receiver before tucking the ball and barreling into the end zone. That gave the 49ersa sweep in the renewal of the instate rivalry with the Rams' return to Los Angeles this season.
"The play he called was a play I think everybody on our offense knew was going to get called, we had great confidence in, and it worked out for us," Kaepernick said. "It felt amazing, a feeling this team has been looking for for a long time now. It is something this team can build on."
Kaepernick threw for 257 yards, two touchdowns and one interception, and rushed for a score for the 49ers (2-13).
Kaepernick found Rod Streater on a slant pattern to cap a 10-play, 73-yard drive to make it 21-20. Streater had six receptions for 63 yards.
Jared Goff was intercepted by Robinson with 16 seconds left as the Rams squandered a late two-score lead for the second time in their past three home games.
Goff, the No. 1 overall draft pick, has lost all six starts for Los Angeles (4-11).
With Goff and the worst offense in the NFL unable to maintain a fast start against the 49ers' league-worst defense, it still appeared the Rams could rely on a strong pass rush and special teams to pick up their second home win.
Punter Johnny Hekker had five punts downed inside the 20-yard line, setting a single-season record in that category. Aaron Donald, Dominique Easley, Eugene Sims and Blake Countess sacked Kaepernick.
However, a reconfigured offensive line for the 49ers, already playing without left tackle Joe Staley and center Marcus Martin before right guard Joshua Garnett suffered a finger injury in the first half, gave Kaepernick time to operate on those final two drives.
The San Francisco defense was even more tenacious, limiting Todd Gurley to 67 yards and forcing a three-and-out to get the ball back to Kaepernick for one last possession.
"What you saw today is what we see every day," Kelly said. "There is an energy and a spirit to them in terms of how they practice and how they train. They have not come up on the right side of things, but that doesn't mean they let that affect them."
Goff was 11 of 24 for 90 yards, finding Tyler Higbee for a 2-yard touchdown in the fourth quarter. He threw his first interception on the Rams' first possession, staring down Kenny Britt and allowing cornerback Tramaine Brock to easily step in front of the receiver.
Brock returned it 38 yards to the Rams 17 and set up a 19-yard touchdown pass from Kaepernick to Carlos Hyde. The San Francisco running back ran out of the backfield to catch the ball just past the line of scrimmage and darted up the middle of the field untouched.
Kaepernick also had a 13-yard touchdown run, making it 21-14 with 5:06 remaining.
"The last few possessions, it seemed like we gave up," Rams cornerback Trumaine Johnson said. "The whole team, including myself."
Kaepernick and Kelly were both quick to praise offensive lineman Zane Beadles for steadying the offensive line, as the seventh-year veteran moved from left guard to center in place of Martin. Beadles also delivered a fiery sermon on the sideline before the49ers' last two drives, possessions where the offense gained 147 of their 323 yards in the game.
"It doesn't matter what position he's put in, what the circumstances are, he is going to step in and do his job," Kaepernick said. "To move to center and also be able to direct traffic and do an amazing job of that just stepping in really speaks to the kind of player he is."
The Rams had not scored a first-quarter touchdown in their previous three games, and had just three touchdowns in the first quarter since Goff replaced Case Keenum as starting quarterback.
But the Rams finished with 177 yards, including just 65 in the final three quarters.
"Our defense is pretty damn good when they get off the field, to get some time when they're going three-and-out every time," Goff said. "Ultimately, the offense needs to play better. It starts with the quarterback, it starts with me, and as a leader of this team, I need to be better. Better than I was today, for sure."
Hyde lost a fumble to set up the Rams' final touchdown, suffering a knee injury on the play that knocked him out of the game. Kelly did not immediately know the extent of the injury.
Britt exited the game in the third quarter with a shoulder injury. Britt had one reception for 15 yards, topping the 1,000-yard mark for the first time in his eight-year career.
49ers: host Seattle next Sunday to conclude the season.
Rams: host Arizona next Sunday to conclude the season.