SANTA CLARA, Calif. - Coach Sean McVay walked into the postgame news conference and immediately asked, "Anyone have a beer?"

He probably wasn't the only person who wanted a drink after watching a surprisingly thrilling Thursday night shootout between his Rams and the San Francisco 49ers that wasn't decided until Los Angeles prevented a potential game-tying 2-point try and then delivered a rare defensive stop after blowing the onside kick in a 41-39 victory.

"We talk about mentally tough, be your best regardless of the circumstance," McVay said. "I thought the players did that. They found a way in spite of some of the ups and the downs to come away with the win."

While the defence came up big late, it was the offence that carried the day for the Rams (2-1), who have gone from the lowest-scoring team in the NFL a year ago to a dynamic one through three games under McVay.

Jared Goff threw for 292 yards and three touchdowns, Todd Gurley ran for 113 yards and scored three TDs and Robert Woods (108) and Sammy Watkins (106) each topped the 100-yard mark receiving in Los Angeles' second 40-point performance of the season. The Rams have 107 points in all so far, the second-most in franchise history after three games to the 119 by "The Greatest Show on Turf" squad in 2000.

"Since I've been here we haven't been able to do that," Gurley said. "Hopefully we can keep putting points together, keep working together and keep learning from this. I think we left a lot more points off the board."

This win didn't come easy as the Rams nearly blew a 15-point lead, giving up two late touchdowns, fumbling a kickoff return and failing to recover an onside kick. But Los Angeles managed to stop a potential game-tying 2-point conversion on a deflection by Troy Hill and then used an offensive pass interference penalty against Trent Taylor and a fourth-down sack by Aaron Donald to stop the Niners after the onside kick.

The 49ers (0-3) scored five touchdowns after failing to get any the first two weeks but still came up short in part because a missed extra point by Robbie Gould forced them to try for 2 on their late touchdown.

"I just rushed it, I missed it, I made a mistake," Gould said. "Obviously, I wish I didn't do that, or we'd probably be playing in overtime right now.""

This time it was a tired defence that hurt San Francisco. After facing 79 plays in a 12-9 loss at Seattle on Sunday, the 49ers appeared to run out of gas on the short week as Goff frequently had wide-open receivers, especially on third down.

The Rams were 8 for 12 on third down, including all three of Goff's touchdown passes.

The Rams needed all that offence on a night where Brian Hoyer threw for 332 yards and two touchdowns and ran for another score.

QUICK START: The Rams took just 12 seconds to get on the board as Nickell Robey-Coleman intercepted Hoyer on the first play from scrimmage and returned it to the 3-yard line. Gurley ran it in on the next play to give the Rams a 7-0 lead.

"I just told him to start over," 49ers coach Kyle Shanahan said. "Got to go back to work. We didn't change anything, went right on with the script. But it was a tough way to start out."

DROUGHT BUSTER: The 49ers came into the game without a touchdown on the season but broke through in the first quarter with some help from the Rams. After Blake Countess jumped offside on a punt, the Niners took advantage of the second opportunity and drove to score on Hoyer's 9-yard run 126:43 into the season. That was the longest it took a team to score its first TD since 2006 when both Tampa Bay (143:03) and Oakland (127:10) took more time.

FOURTH DOWN CALLS: Both teams drove to the opposing 1 on their opening drives of the second half with help from a Willie Mays-style basket catch by Watkins and a perfect toe drag on the sideline by San Francisco's Pierre Garcon. But the Rams opted to kick a short field goal, while the 49ers went for it and converted on Carlos Hyde's 1-yard run that cut Los Angeles' lead to 27-20. Hyde added a second 1-yard run on fourth down in the fourth quarter.

INJURIES: Rams S Lamarcus Joyner left the game in the first half with a hamstring injury. ... Los Angeles C John Sullivan injured his groin in the second half and Watkins and Tavon Austin left with concussions. ... 49ers S Jaquiski Tartt (concussion), FB Kyle Juszczyk (neck), DL Tank Carradine (ankle) and LB Brock Coyle (concussion) all left with injuries in the second half.

UP NEXT: The Rams travel to Dallas on Oct. 1. The 49ers visit Arizona.