SANTA CLARA, Calif. -- Jared Goff passed for 292 yards and a career-high-tying three touchdowns, and Todd Gurley II scored three TDs as the Los Angeles Rams held on for a wild 41-39 victory against the San Francisco 49ers on Thursday night at Levi's Stadium.
The Rams (2-1) ended a three-game losing streak to the 49ers (0-3), who remained winless under new coach Kyle Shanahan.
San Francisco trailed 41-26 late in the fourth quarter, but two touchdowns cut the deficit.
Running back Carlos Hyde's 1-yard run pulled the 49ers within 41-39 with 2:13 to go, but the two-point try failed when nose tackle Michael Brockers intercepted Hoyer's deflected pass intended for wide receiver Trent Taylor.
Gurley rushed for 113 yards and two touchdowns on 28 carries, and he caught five passes for 36 yards and a score. Rams wide receiver Sammy Watkins caught six passes for 106 yards and two TDs, while wideout Robert Woods had six catches for 108 yards.
Goff, who grew up in Marin Country, north of San Francisco, and starred in college at California, completed 22 of 28 passes with a passer rating of 145.8.
San Francisco quarterback Brian Hoyer completed 23 of 37 passes for 332 yards and two touchdowns -- his first two as a 49er -- and ran for a score. He was intercepted once.
Wide receiver Pierre Garcon caught seven passes for 142 yards for the 49ers. Hyde rushed for 84 yards and two touchdowns on 25 carries.
The Rams stalled at the San Francisco 1 on their first drive of the second half and settled for Greg Zuerlein's 19-yard field goal, extending their lead to 27-13 with 9:17 remaining in the third quarter.
San Francisco cut its deficit to 27-20 when Hyde scored on a 1-yard blast on fourth-and-goal later in the third. But the Rams answered with Goff's 1-yard touchdown pass to Sammy Watkins, making it 34-20 entering the fourth quarter.
The 49ers drove 75 yards in six plays for Hoyer's 1-yard touchdown pass to tight end Garrett Celek with 12:50 left to play. Robbie Gould missed the extra point, leaving the Rams with a 34-26 advantage.
The Rams extended their lead to 41-26 on Goff's 13-yard TD pass to Watkins with 8:43 left. Hoyer answered with a 3-yard touchdown pass to Taylor with 5:08 to play, cutting the Rams' lead to 41-33.
On the ensuing kickoff, 49ers wide receiver Victor Bolden Jr. stripped the ball from Rams return man Pharoh Cooper, and San Francisco's Aldrick Robinson recovered at the Rams' 29.
On fourth-and-goal from the 1, Hyde rammed into the end zone, setting up the failed two-point try.
San Francisco's Raheem Mostert recovered Gould's onside kick at the 50 with 2:10 left. However, the 49ers backpedaled, and Rams defensive tackle Aaron Donald sacked Hoyer at San Francisco's 32 for an 8-yard loss on fourth-and-20.
Gurley ran for two touchdowns and caught a TD pass in the first half as the Rams built a 24-13 lead.
Los Angeles took a 7-0 lead only 12 seconds into game, capitalizing on a 49ers turnover on the game's first play from scrimmage.
Rams cornerback Nickell Robey-Coleman stepped in front of Hoyer's short pass intended for Marquise Goodwin for an interception and returned 25 yards to the 3.
On first down, Gurley blasted up the middle for a touchdown.
The 49ers pulled even on Hoyer's 9-yard touchdown run with 8:17 left in the opening quarter, San Francisco's first touchdown of the season.
The Rams moved ahead 14-7 when Gurley scored on a 7-yard pass from Goff with 4:32 left in the first quarter. The Rams made it 17-7 on Zuerlein's 48-yard field goal with 14:55 remaining in the first half as they took advantage of 49ers running back Raheem Mostert's fumble at the Los Angeles 32.
San Francisco cut the Rams lead to 17-13 on a pair of Gould field goals, the first from 36 yards and the next from 48 with 3:46 left in the half.
The Rams increased their lead to 24-13 on Gurley's 2-yard touchdown sweep with 49 seconds left in the half, capping a 75-yard drive.
NOTES: 49ers SS Eric Reid (strained knee), who was injured Sunday against Seattle, was inactive, and S Jaquiski Tartt started in his place. Tartt sustained a head injury in the third quarter and left the game. ... 49ers OLB Ray-Ray Armstrong started in place of Reuben Foster (sprained ankle), who was inactive for the second straight game. ... Rams CB Kayvon Webster (shoulder) was inactive, and Nickell Robey-Coleman made his second start of the season.