The Rams scored 17 consecutive points in the second half and seemed to have grabbed control. The 49ers, though, weren’t finished — just as their season isn’t finished.

49ers kicker Robbie Gould kicked two field goals in the final 3:11 as San Francisco upset the Rams 23-20. The 49ers swept the season series, winning the first meeting 24-16 on Oct. 18.

The Rams fell to 7-4 on a day the Cardinals also lost to fall to 6-5. The Seahawks, who are 7-3 and now alone in first place in the NFC West, play the Eagles on Monday night. The 49ers are 5-6 after winning a fourth consecutive game against the Rams.

Gould kicked a 44-yarder with 3:11 left to tie the game. The Rams punted after only 19 yards on the next possession, and the 49ers got Gould in position for the game-winner. He kicked a 42-yarder on the final play after Jalen Ramsey jumped offsides to get him 5 yards closer.

The 49ers went for it on fourth-and-one on the Rams 39 with 35 seconds left, and fullback Kyle Juszczyk gained 2 yards. That allowed the 49ers to move closer and eat up the remaining time.

The teams combined for 14 punts and seven turnovers. Both teams scored defensive touchdowns, with 49ers rookie Javon Kinlaw scoring on a 27-yard interception return and Rams cornerback Troy Hill returned a fumble forced by Aaron Donald 20 yards for a touchdown.

Donald had five tackles, a sack, four quarterback hits, a pass breakup and a forced fumble.

Jared Goff, though, had three of the Rams’ four turnovers. Kinlaw and Richard Sherman had interceptions of Goff, and Goff lost a fumble that Jimmie Ward forced and Kerry Hyder recovered. Malcolm Brown also lost a fumble.

Nick Mullens threw an interception and 49ers running backs Jeff Wilson and Raheem Mostert each lost a fumble.

49ers receiver Deebo Samuel set career highs in receptions and yards with 11 catches for 134 yards.

Mullens was 24-of-35 for 253 yards and the one pick, while Goff was 19-of-31 for 198 yards and the two picks.

Cam Akers had nine carries for 84 yards and a touchdown for the Rams.