The win helps the Bolts avoid a winless preseason after losing their first three matchups.
Philip Rivers again stayed on the sidelines, letting backup quarterbacks Cardale Jones and Easton Stick divide the reps for the preseason finale.
Jones, the former Ohio State standout, had a solid performance in the first half. He threw for 149 yards and ran in one touchdown to help build a 20-14 lead at the half.
Stick then struggled in the second half, throwing for just 22 yards as the 49ers knocked in a field goal and rushing touchdown to take the lead with 5:40 left in the fourth quarter.
With 1:17 to go, Derrick Gore helped the Bolts retake the lead, smashing his way through an 11-yard rush to the end zone.
An interception by safety Adarius Pickett with less than a minute to go sealed the win.
Detrez Newsome provided a rushing touchdown in the second quarter and Mike Badgley knocked in two field goals.
The Chargers, who finished 12-4 last season, had rested Rivers and many of their other key starters through the preseason.
Before the game, Chargers coach Anthony Lynn said the matchup was still important even if it didn't count toward the team's regular-season record.
"It does matter," coach Anthony Lynn said. "It's a game. They keep score for a reason and we want to come out on top. I made a decision before camp that there were certain guys that were not going to play, and some guys that were going to play but not very much. I looked at our team last year at the end of the season, I thought we were a little tired. We had some injuries here and there and some guys were worn down. I just feel like it's going to help in the long run."
San Francisco headed into the game looking to resolve the backup quarterback situation between Nick Mullens and C.J. Beathard. Coach Kyle Shanahan said the Niners could keep three quarterbacks this season for the first time in his three years as head coach because he doesn't want to give up on either of his backups.
The Chargers' regular season opens Sept. 8 hosting the Indianapolis Colts at Dignity Health Sports Park in Carson.