SANTA CLARA, Calif. (AP) An exhilarating, game-winning drive that put the San Francisco 49ers in position to seize the top seed in the playoffs next week would normally be reason to celebrate.

The mood for the 49ers was far from joyous following a 34-31 victory Saturday night over the Los Angeles Rams because teammates were more concerned about the well-being of third-string quarterback C.J. Beathard, whose younger brother Clayton was fatally stabbed earlier in the day at a Nashville bar.

''It was definitely the toughest game of my career,'' said an emotional George Kittle, who played with Beathard in college at Iowa and was close to the family.

''Today was absolutely terrible, for me the entire day. It sucked all around. Overall it was just kind of a brutal day. We got that one for C.J. and I'm so happy we were able to do it.''

It didn't come easy, as it took Jimmy Garoppolo converting a pair of conversions on third-and-16 to set up Robbie Gould's game-winning 33-yard field goal with no time left to eliminate the defending NFC champion Rams from playoff contention.

Garoppolo made up for a mostly pedestrian game for the 49ers (12-3) by delivering two big throws on the final drive after taking his fifth and sixth sacks.

''To convert two of those the way we did it was pretty incredible,'' Garoppolo said.

He found Kendrick Bourne on an 18-yard pass over the middle to convert the first long third down and then hit Emmanuel Sanders behind a blown coverage for 46 yards.

San Francisco had failed on 50 straight third downs when needing at least 16 yards to convert since last doing it in Week 3 in 2017.

''Usually it's such a low percentage that you don't even try on third and 16,'' coach Kyle Shanahan said. ''Usually you just try to survive the down and get half of it.''

The Niners then ran down the clock before Gould made the kick that knocked the Rams (8-7) out of the playoffs. Minnesota became the fifth team to clinch a berth in the NFC with Dallas and Philadelphia battling for the final spot.

''It's extremely disappointing,'' Rams quarterback Jared Goff said. ''You never envision something like this when you start the year. It's not even close to the standard we want to hold. It's sickening.''

The victory kept the Niners in control in the race for the NFC's top seed and home-field advantage throughout. They still need to win next week at Seattle to win the NFC West, otherwise they will be relegated to wild-card status.

Garoppolo finished 16 for 27 for 248 yards with one touchdown and two interceptions.

Goff connected on a 22-yard TD pass to Cooper Kupp in the third quarter to put the Rams ahead 28-24. The defense had throttled the Niners, holding them to four punts, one interception and only two first downs in the second half before San Francisco took over at the 9 midway through the fourth quarter.

Garoppolo hit Kittle on a 36-yard pass to start the drive and then connected with Ross Dwelley on a 25-yarder that also featured a personal foul on Eric Weddle. Three plays later, Garoppolo scrambled on third down to give Kittle time to work free and then found him in the front of the end zone for the touchdown that made it 31-28.

Goff responded and drove the Rams to a tying 52-yard field goal with 2:30 to play after Marcell Harris dropped a potential interception for San Francisco.

But the Rams couldn't get another stop and lost

''Bottom line is you had two third down and I want to say 16s where we had a chance to get those guys off the grass, the way that we had played the whole night,'' coach Sean McVay said. ''I think those two plays are not indicative of the effort that our defense gave and it's unfortunate that they ended that way, because they had put together a really good performance against a tough offense.''

The offenses controlled the game early with Goff throwing a 10-yard TD pass to Brandin Cooks on the opening drive and Todd Gurley scoring on two short runs to stake the Rams to a 21-10 lead.

But Raheem Mostert answered Gurley's second score with a TD run of his own. The defenses then buckled down starting when Fred Warner intercepted a swing pass from Goff in the closing minute of the first half and returned it 46 yards for a score that put San Francisco up 24-21 at the half.

The Niners defense then held firm after Garoppolo's second interception set up the Rams in prime position to start the second half. But an intentional grounding penalty led to a 52-yard missed field goal by Greg Zuerlein.

STREAKING

Mostert scored on a 16-yard run in the second quarter to give him a rushing touchdown in five straight games. He's the first 49ers player to do that since Derek Loville had a six-game streak in 1995. The only other San Francisco players to do that are Joe Perry (seven games in 1953-54) and Hugh McElhenny (five in 1954).

INJURIES

Rams: Jalen Ramsey left with a knee injury in the first half and was initially listed as doubtful to return but came back the following series. ... LB Kenny Young (ankle) and RB Darrell Henderson (ankle) also left with injuries.

49ers: S Tarvarius Moore left the game with a concussion.

UP NEXT

Rams: Host Arizona on Sunday.

49ers: Visit Seattle on Sunday.

---

More AP NFL: https://apnews.com/NFL and https://twitter.com/AP-NFL

Copyright 2019 by STATS LLC and Associated Press. Any commercial use or distribution without the express written consent of STATS LLC and Associated Press is strictly prohibited.