CHICAGO — Blaine Gabbert is building a case for a starting job with the San Francisco 49ers next season.

Gabbert threw a 71-yard touchdown pass to Torrey Smith in overtime after running for the tying TD late in regulation, lifting the struggling 49ers to a 26-20 victory over the Chicago Bears on Sunday.

The Bears (5-7) had just gone three-and-out after forcing a punt by San Francisco to start the extra period.

On the first play of the 49ers’ second possession, Gabbert found a wide-open Smith along the left sideline, giving San Francisco (4-8) a surprising win over a team that had jumped into playoff contention.

All that happened after a wild finish in regulation.

The Bears took a 20-13 lead on Ka’Deem Carey’s 4-yard run with 3:32 left, only to let it disappear and then blow a chance to win the game as time expired.

advertisement | advertise on newsday

They let Gabbert scramble untouched through the middle for a 44-yard touchdown with 1:42 left.

If that wasn’t bad enough for Chicago, the Bears wasted a huge opportunity after Deonte Thompson returned the kickoff 74 yards to the 28. The Bears had the ball at the 17 when the usually reliable Robbie Gould, who missed a 40-yard attempt in the third quarter, booted a 36-yarder wide left as time expired.

Gabbert, who took over for Colin Kaepernick last month, completed 18 of 32 passes for 196 yards and a touchdown. He also ran for 75 yards and his score.

Smith had two catches for 76 yards, and Jimmie Ward returned his first career interception for a touchdown, helping the 49ers snap a two-game losing streak and secure their first road win. They were 0-5 away from home.

Chicago’s Jay Cutler was 18 of 31 for 202 yards with that interception. Alshon Jeffery had 85 yards receiving and Matt Forte ran for 84 and a touchdown. But after picking up a rare win at Green Bay on Thanksgiving, the good vibes in Chicago came screeching to an end with a gut-wrenching loss against a team both coordinators would have loved to beat.

NFL videos

Defensive coordinator Vic Fangio and offensive coordinator Adam Gase interviewed for the 49ers’ head coaching job that went to Jim Tomsula following Jim Harbaugh’s departure after last season. Gase wound up following Fox from Denver, and Fangio moved to Chicago after overseeing one of the league’s top defenses the past four years in San Francisco.