The Lions lost for the first time this year and Detroit Coach Jim Schwartz became involved in a post-game altercation with his San Francisco counterpart Jim Harbaugh.
The outcome left both teams with a 5-1 record and the 49ers' road victory put them firmly atop the NFC West.
Harbaugh, coaching his sixth NFL game, excitedly slapped Schwartz' hand as they met on the field after the contest and Harbaugh then swatted Schwartz on the back as he headed for the locker room. Schwartz later said Harbaugh had tried to push him out of the way and had used an obscenity.
Schwartz took exception to Harbaugh's actions and ran after the San Francisco coach, yelling at him as he did so with television cameras zooming in on the scene. Team officials ran along between the two coaches as they departed the field to make sure the incident did not escalate.
Walker's go-ahead touchdown on a pass from Alex Smith gave San Francisco a 22-19 lead and Detroit failed to move the ball after the ensuing kickoff.
San Francisco took over at the Detroit 25 following a failed fourth-down pass, but the Lions used their time outs to preserve the clock and the 49ers wound up getting a 37-yard field goal from David Akers with 1:06 remaining.
Detroit again ran four plays without success to end the game.
Frank Gore gained 141 yards on 15 carries and scored a touchdown for San Francisco, which is off to its best start after six games in 13 years. Smith completed 17-of-32 passes for 125 yards.
Detroit's Matthew Stafford was 28-of-50 for 293 yards and two touchdowns. The Lions had been trying for its first 6-0 start since 1956.
Swim Week Miami Beach 2014 [PHOTOS]