DETROIT (CBS/AP) — Matthew Stafford and the Detroit Lions took advantage of several mistakes by the San Francisco 49ers.

Stafford threw for two touchdowns, including one to Calvin Johnson early in the fourth quarter, and Detroit beat San Francisco 32-17 on Sunday.

READ MORE: Most Popular Dog And Cat Names Of 2021: Is Your Pet's Name On The List?

The Lions (6-9) have won five of their last seven games, perhaps giving coach Jim Caldwell a chance to keep his job for a third season.

The 49ers (4-11) have lost three straight and five of six under first-year coach Jim Tomsula.

Stafford’s 1-yard TD pass to Johnson gave Detroit a 12-point lead one play after Tramaine Brock was called for pass interference in the end zone. During the drive, the 49ers were flagged for being offside or in the neutral zone for the seventh time to set a single-game high by an NFL team this year, according to STATS.

San Francisco turned the ball over on downs on its next two possessions.

The 49ers became the first team to have six offside or neutral-zone penalties in the first half since 1993, when the Houston Oilers did it against New Orleans, according to STATS. On special teams, they allowed Detroit to convert a fake punt, missed a field goal and negated a pair of 30-plus yard returns in the closely contested first half with penalties. San Francisco was flagged 11 times for the second straight week.

Blaine Gabbert threw for two TDs, both of which gave San Francisco a lead, but his fumble let the Lions take the lead for good.

Joique Bell’s 1-yard run midway through the second quarter gave Detroit a 17-14 lead one play after Gabbert lost the ball on a sack.

READ MORE: COVID Omicron Variant: CDC Expands Surveillance To San Francisco International Airport

Stafford was 29 of 37 for 302 yards. Johnson, possibly playing his last home game with the Lions because they may cut him to manage their salary cap, had six receptions for 77 yards.

Gabbert was 22 of 33 for 225 yards. Anquan Boldin became the 13th NFL player with 1,000 career receptions on the first play of the game, caught all five of the passes thrown his way for 27 yards in the first quarter and went without another catch the rest of the game. DuJuan Harris, signed by San Francisco on Tuesday off Baltimore’s practice squad, ran for a career-high 73 yards while replacing injured running back Shaun Draughn.

Harris had 74 yards on nine carries in the first half when the 49ers trailed 20-17, and then carried the ball just two more times in the second half.

He helped San Francisco to a relatively successful first half in which it tied a season high for points by halftime.

For the first time this season, San Francisco’s offense scored a TD in the first quarter when Gabbert threw a 1-yard pass to Vance McDonald.

Detroit’s Matt Prater kicked his first of four field goals on the ensuing drive.

Caldwell, perhaps coaching the Lions for the last time at home, called for a fake punt and the gamble paid off. He also challenged a spot later in the game that gave Detroit a first down.

Isa Abdul-Quddus, Detroit’s upback on punts, took a direct snap and converted a fourth-and-1 from the Detroit 35. Stafford went on to throw a 29-yard pass to TJ Jones for his first TD reception to put the Lions up 10-7 early in the second quarter.

MORE NEWS: San Francisco Bay Area Bridge Tolls To Jump By $1 On Jan. 1

© Copyright 2015 The Associated Press. All Rights Reserved. This material may not be published, broadcast, rewritten or redistributed