LOS ANGELES --Colin Kaepernick threw a 10-yard touchdown pass with 31 seconds remaining and scrambled for a go-ahead 2-point conversion and the San Francisco 49ers overcame a 14-point deficit to end a 13-game losing streak with a 22-21 win over the Los Angeles Rams at the Coliseum on Saturday.
Kaepernick rolled to his right while looking for a receiver before tucking the ball and barreling into the end zone to give the 49ers a sweep in the renewal of the instate rivalry with the Rams' return to Los Angeles this season.
Kaepernick threw for 257 yards, two touchdowns and one interception and rushed for a touchdown 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.
Jared Goff was intercepted by Rashard Robinson with 16 seconds left as the Rams again 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 of his 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 appeared the Rams would be able to 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, as he set a single-season record in that category, while Aaron Donald, Dominique Easley, Eugene Sims and Blake Countess sacked Kaepernick.
Goff was 11 of 24 for 90 yards, finding Tyler Higbee for a 2-yard touchdown in the fourth quarter and throwing his first interception on the Rams' first possession, staring down Kenny Britt and allowing cornerback Tramaine Brock to easily step in front of the ball.
Brock returned it 38 yards to the Rams 17-yard line and set up a 19-yard touchdown pass from Kaepernick to Carlos Hyde two plays later.
Hyde ran out of the backfield to catch the ball just past the line of scrimmage and darted up the middle of the field untouched for his third touchdown reception of the season.
Kaepernick also had a 13-yard touchdown run, making it 21-14 with 5:06 remaining.
After falling behind 7-0, the Rams responded with scores on their next two drives. Pharoh Cooper returned the ensuing kickoff 51 yards, and the nine-play drive was capped off by a 1-yard touchdown run by Todd Gurley.
Goff had a 15-yard pass to Tavon Austin on third down and found Lance Kendricks for a 24-yard catch-and-run on a subsequent fourth down.
Gurley rushed for 67 yards.
After Kaepernick overthrew Garrett Celek on a ball that was picked off by safety Cody Davis, Austin took a sweep 30 yards for a touchdown on the next play.
The Rams had not scored a first-quarter touchdown in their previous three games, and had scored just three touchdowns in the first quarter since Goff replaced Case Keenum as starting quarterback.
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.
The Rams finished with 177 yards, including just 65 in the final three quarters.