Kelley L Cox-USA TODAY Sports

Christian McCaffrey scored on a 2-yard run with 7:54 remaining to give the San Francisco 49ers their first lead on the way to a 22-16 victory Sunday night over the visiting Los Angeles Chargers.

McCaffrey, who played in his third game with the 49ers after he was acquired in a trade from the Carolina Panthers, rushed for 38 yards and scored his fourth TD for San Francisco, including a passing score in Week 8. Elijah Mitchell had 89 yards rushing for the 49ers.

Jimmy Garoppolo had 240 yards passing and a rushing TD for the 49ers (5-4). After missing a game because of a hamstring issue, 49ers wideout Deebo Samuel had two receptions and 27 yards rushing.

Justin Herbert passed for 196 yards with a touchdown and an interception for the Chargers (5-4), who scored a touchdown on their opening drive and then never found the end zone again. Austin Ekeler had seven receptions and 24 rushing yards for the Chargers.

The Chargers continued to play without proven wideouts Keenan Allen (hamstring) and Mike Williams (ankle).

After McCaffrey's late touchdown gave the 49ers a 19-16 lead, Robbie Gould made a 20-yard field goal with one minute remaining for the final margin. The Chargers had one last drive but Herbert was intercepted by Talanoa Hufanga.

Despite the 49ers' offensive firepower, it was the Chargers that got off to the fast start, taking a 7-0 lead when Herbert capped the opening seven-play, 75-yard drive with a 32-yard touchdown pass to DeAndre Carter.

The 49ers received a 20-yard field goal from Gould on their first drive, before the Chargers matched that on the second play of the second quarter with a 47-yard field goal from Cameron Dicker.

The 49ers found the end zone for the first time on Garoppolo's 1-yard TD run with 1:57 remaining in the first half to pull within 13-10. The Chargers took a 16-10 lead into halftime after Dicker's 40-yard field goal.

Los Angeles played without defensive tackle Otito Ogbonnia, who had a left leg injury on the 49ers' opening drive. Chargers defensive tackle Christian Covington left in the third quarter and did not return.

San Francisco linebacker Dre Greenlaw was ejected with 30 seconds remaining in the first half after a helmet-to-helmet hit on Herbert.

More must-reads:

TODAY'S BEST
Mavericks advance to Western Conference Finals aided by controversial call late
Connor McDavid, Oilers hammer Canucks to force Game 7
Tyson Fury-Oleksandr Usyk epic increases excitement for potential rematch
Seize the Grey wins in muddy Preakness
Even Mike Budenholzer admits the Suns need a point guard
Watch: Juan Soto's first multi-homer game as a Yankee
Xander Schauffele, Collin Morikawa lead at PGA Championship
Knicks could get major boost for Game 7 showdown with Pacers
Giants All-Star pitcher suffers setback in recovery from injury
Panthers star named winner of 2024 Selke Trophy
WNBA to investigate $100,000 sponsorship deals for Aces players
Tiger Woods blames one big factor for missing the cut at PGA Championship
'Ain't good enough': Draymond Green claims Celtics must 'win it all' or it's a 'failure'
Blue Jays GM wants struggling club to feel 'massive sense of urgency'
Raptors expected to flip former NBA champion during the offseason
MLB insider reveals Mets' massive extension offer that Pete Alonso turned down
Celtics legend provides update after gruesome finger injury
Bulls hire former NBA head coach as top assistant
Chiefs move on from young running back
20-year MLB veteran working out, unsure about playing future

Want more NFL news?

Join the hundreds of thousands of fans who start their day with Yardbarker's Morning Bark, the best newsletter in sports.