Christian McCaffrey ran for the go-ahead touchdown with 7:54 to play, and the San Francisco 49ers beat the Los Angeles Chargers 22-16 in their first game back from a bye week.