Click here if you’re unable to view the video or photo gallery on your mobile device.

BALTIMORE — Tied at 17 and faced with fourth down in enemy territory, that is as close as the 49ers would come to upsetting the host Ravens on Sunday.

Jimmy Garoppolo’s fourth-and-1 pass got batted away at the line with 6 1/2 minutes remaining, and the 49ers never got the ball back.

The Ravens marched for a Justin Tucker 49-yard field goal as time expired to win 20-17 in a riveting, back-and-forth matchup pitting conference leaders.

“We need to make more plays in crucial situations, and that’s what separates a lot of these guys, a couple plays here and there,” Garoppolo said as the 49ers fell to 10-2 with a NFC showdown looming next Sunday in New Orleans (10-2).

On the bright side, Raheem Mostert ran for a career-high 146 yards, and he churned out three consecutive 3-yard gains before the 49ers opted to ignore him on the fourth-and-1 call, which was preceded by a timeout.

“I didn’t mind the decision at all of not running, but I wish I had called a better pass play,” said Shanahan, noting that the Ravens (10-2) inundated the line of scrimmage with defenders to stop a potential run.

Shanahan and several 49ers took pride in the gutty effort and how their fate wasn’t sealed until the final snap, like their only other loss three weeks ago to Seattle, which won on an overtime field goal.

Ravens quarterback Lamar Jackson predictably flummoxed the 49ers, especially with his zone-read keepers around the edge, and he finished with 101 rushing yards and 105 passing yards.

But the 49ers did halt Jackson once to halt Baltimore’s momentum: Marcell Harris stripped Jackson of the ball on a potential touchdown run in the third quarter.

“That was one of the big turning points in the game,” Shanahan said. “I think the next turning point was us going for it on fourth-and-1.”

Simply, the 49ers couldn’t recreate the fourth-down magic that worked on the game’s opening drive, when Jimmy Garoppolo threw a 33-yard touchdown pass to Deebo Sanders on fourth-and-2.

Now the 49ers must recover from their second loss in four games, and they’ll head to Bradenton, Fla., to practice this week ahead of next Sunday’s NFC showdown at the New Orleans Saints (10-2).

STUDS:

RB Raheem Mostert: A 2015 rookie with the Ravens, Mostert ran for a career high 146 yards (19 carries) and became the 49ers’ third different back to eclipse 100 yards in a game this year. He sparked his breakout game with a 40-yard, game-tying touchdown 9:12 before halftime. It was his third touchdown of the season, and it came with great blocks by Mike McGlinchey, George Kittle, Richie James and Mike Person. Mostert’s other big runs went for 19, 18 and 16 yards.

“I didn’t really have a good experience when I was here, but I made the most of my opportunities when I was,” Mostert said of his 2015 Ravens tenure. “I wasn’t holding on to a grudge but I wanted to show the organization what they missed out on, and it worked to my benefit.”

SS Marcell Harris: He hijacked the football from Lamar Jackson’s grasp in the red zone, ending a 14-yard run and saving the 49ers defense on the first series after halftime. Jackson had just turned the corner on Nick Bosa, and Harris shed Mark Andrews’ block at the perfect time to surprise Jackson.

“I saw Lamar coming, so I tried to stay outside. When he went back inside, I was able to get my hand on the ball and rip that thing out,” Harris said.

It was Harris’ first career forced fumble and fumble recovery, and it came on his fourth snap after replacing an injured Jaquiski Tartt (ribs).

QB Jimmy Garoppolo: He chucked a 33-yard touchdown pass to Deebo Samuel — on fourth-and-2, no less — to cap the game-opening drive and rebound after a third-down sack. Fumbling on a third-down sack on the next series was not so stud-like, however. Garoppolo found Emmanuel Sanders for an 8-yard, third-down conversion to set up Mostert’s TD on the next snap. He finished 15-of-21 for 165 yards.

“It was a good challenge today,” Garoppolo said. “They’re a good team, Baltimore, they definitely are. We have room for improvement and plenty of things we can do better.”

WR Deebo Samuel: Not only did he produce a 33-yard touchdown catch on fourth-and-2, he pushed past a flat-footed Marcus Peters at the 5-yard line. “I wasn’t even looking at him. I was looking at the ball,” Samuel said.

It was Samuel’s third touchdown of the season, tying him for the team lead with Kittle and Kendrick Bourne. Samuel opened up a second-quarter series with a 20-yard run.

LB Fred Warner: He dove to break up a fourth-and-5 pass in 49ers territory with 9:38. It was reminiscent to a first-series play in which he broke up a 20-yard pass intended for Marquise Brown. Not so studly: losing track of tight end Mark Andrews on the Ravens’ game-tying TD (and on ensuing snaps, too). Warner had 11 tackles.

WR Kendrick Bourne: He nabbed a low pass and tooki it upfield for 30-yard gain on third-and-7, their first third-down play of the game. He finished with a team-high 42 yards on three catches; Samuel and Emmanuel Sanders each had 41 yards.

DUDS:

Fourth-down call: The 49ers took a timeout to think over a fourth-and-1 call, and rather than let Mostert try to add to his 146 yards, they had Garoppolo attempt a pass over the middle to George Kittle. The pass got batted away at the line, the Ravens took possession with 6:28 remaining.

“There were a couple different options. George, I thought, had the best chance,” Garoppolo said. “We had an opportunity at him, and the ball got tipped.”

Earlier in the game, Garoppolo earlier converted on a third-and-1 keeper, so why not try that again? “Third-and-1 wasn’t the same (defensive) looks that they have,” Shanahan said. “We saw on the original fourth-and-1 (before the timeout), they committed everyone in there, and it’s extremely hard. Also, we thought it was a full yard, so it’s not always great to sneak it.”

Run defense: Edge defenders repeatedly bit on Lamar Jackson’s zone-read keepers, including Arik Armstead, Nick Bosa, Solomon Thomas and K’Waun Williams, the latter of whom likely will be part of Jackson’s MVP highlight reel because of that deceptive 8-yard run. Allowing 51 yards through the first quarter was a bad sign, especially in that the Ravens surprisingly succeeded with runs up the middle where the 49ers should be stronger with DeForest Buckner and D.J. Jones. On Jackson’s 1-yard, go-ahead touchdown run, linebacker Mark Nzeocha broke into the backfield and allowed Jackson to escape easily across the goal line.

“I wouldn’t say they’re the quickest (opponent yet), but it was all 11 at the ball at all times every play, so you’ve got to give them credit, they were flying,” Jackson said.

Tight end coverage: Jackson found tight ends for 75 of his first 105 passing yards, including big strikes to Mark Andrews (12 yards) and Hayden Hurst (10 yards) that got them in range for Tucker’s game-winning kick. Earlier, Andrews had 20-yard touchdown catch and Hurst made a fourth-and-2 conversion.

Roughing calls: Linebacker Azeez Al-Shaair and defensive tackle Sheldon Day each got called for roughing Jackson. Al-Shaair’s offsett a third-down incompletion and kept alive an 84-yard touchdown drive.

CB Ahkello Witherspoon: Making his first start since Week 3, he looked lost and didn’t attempt to break up Andrews’ touchdown catch.

Andrews’ celebration: He gathered up the Ravens to plant an imaginary flag. It was a shot at Nick Bosa, who did that celebration in Week 5 to get back at Baker Mayfield, Andrews’ former Oklahoma teammate.

SCORING RECAP:

Jimmy Garoppolo’s fourth-down touchdown pass to Deebo Samuel gave the 49ers a startling 7-0 lead, the first points scored on an opening drive by a Ravens opponent this season.

The Ravens pulled even at 7 when they eventually converted a Garoppolo fumble (on a third-down sack) into a Lamar Jackson touchdown pass to Mark Andrews. Jackson’s 1-yard touchdown run on the Ravens’ next series put them ahead 14-7.

The 49ers punched back with a 40-yard touchdown run by Raheem Mostert to square things 9:12 before halftime.

A 30-yard field goal by Justin Tucker put the Ravens ahead 17-14 1:58 before halftime, that drive enhanced by a Sheldon Day roughing penalty for hitting Jackson low.

Robbie Gould’s 32-yard field goal tied it at 17 with 3:37 left in the third quarter, capping a drive that included a fourth-down conversion via a pass-interference penalty.

Justin Tucker’s 49-yard field goal as time expired capped a 12-play, 34-yard drive that ate up the final 6 1/2 minutes.