Robbie Gould made a 42-yard field goal as time expired, and the San Francisco 49ers snapped their three-game losing streak with a 23-20 victory over the Los Angeles Rams on Sunday, sweeping their season series for the second straight year.

Nick Mullens passed for 253 yards and led two late scoring drives in his first victory as a starter since September for the defending NFC champion Niners (5-6), who stoked their flickering playoff hopes and became the first team to beat the Rams (7-4) at new SoFi Stadium.

After a game largely dominated by defense, Gould hit a 44-yard field goal with 3:11 left before San Francisco stopped Los Angeles near midfield. Fullback Kyle Juszczyk converted a fourth-and-1 with 28 seconds to play during a 56-yard drive in the final 2:10 to set up Gould, who nailed his third field goal of the day after a Rams offside call moved him 5 yards closer.

CLICK HERE FOR MORE SPORTS COVERAGE ON FOXNEWS.COM

Rookie defensive lineman Javon Kinlaw returned an interception 27 yards for a touchdown as San Francisco forced four turnovers, including two interceptions and a fumble by Jared Goff in his latest erratic performance.

Aaron Donald forced a fumble and Troy Hill returned it 20 yards for a touchdown late in the third quarter, but the Rams' top-ranked defense couldn't keep the Niners out of field goal range late.

Goff passed for 198 yards in a rough overall offensive game from the Rams, who slipped out of first place in the division with their fourth straight loss to their upstate rivals. Rookie Cam Akers scored the Rams' go-ahead touchdown early in the fourth quarter, but Los Angeles managed just 34 yards on its final two drives.

San Francisco's hopes of a second straight Super Bowl trip have been hurt by injuries, but several key players returned to boost this impressive win.

Raheem Mostert scored the Niners' opening touchdown in his return from a four-game absence with a sprained ankle, and Richard Sherman had an interception in his first game since the 49ers’ season opener. Deebo Samuel had 11 catches for 134 yards in his return from a hamstring injury.

The defenses largely dominated in the Niners' first visit to the Rams' multibillion-dollar new stadium. The teams combined for four takeaways in the first quarter alone, with the Rams committing two turnovers on their first three drives before Goff fumbled late in the first half.

San Francisco had an early fumble before Mullens threw an interception to Rams rookie Jordan Fuller.

Kinlaw scored his first career TD on his first career interception in his first game back from the reserve/COVID-19 list, rumbling into the end zone after Goff was hit as he threw on the second play of the second half.

The Rams answered with their own defensive touchdown set up by Donald, who had gone without a tackle in two straight games for the first time in his career. The All-Pro lineman was an unstoppable force against San Francisco, and he stripped the ball from Mostert for Hill to return it untouched.

Los Angeles' offense finished its first TD drive with Akers' run early in the fourth quarter after Akers set it up with a 61-yard run.

INJURIES

49ers: Nickel back Jamar Taylor left the field on a cart late in the second quarter with an apparent left knee injury. ... CB Ken Webster also left on the cart in the fourth quarter.

Rams: LB Troy Reeder had 15 tackles in place of leading tackler Micah Kiser.

UP NEXT

49ers: Host the Buffalo Bills somewhere on Dec. 7, but probably not at Levi's Stadium due to Santa Clara County's coronavirus restrictions.

Rams: Visit the Arizona Cardinals next Sunday.