Jeff Wilson Jr. also scored on a 32-yard run that gave the Niners (2-2) their seventh straight regular-season win over their in-state rivals.
The Rams (2-2) won the matchup that meant most in last season's NFC championship game on the way to a Super Bowl title, but this meeting looked more like their recent regular-season meetings.
San Francisco used a relentless defense to slow down coach Sean McVay's offense and got enough big plays for the win.
Nick Bosa had two of San Francisco's seven sacks against Matthew Stafford to give him an NFL-best six on the season and Hufanga iced it with his 52-yard pick-6 in the fourth quarter.