The Ravens converted a fourth down from their own 44-yard line with under five minutes remaining and went on to run down the clock before Tucker's game-clincher.
Lamar Jackson had a season-low 105 passing yards on the day, but managed 101 on the ground, a touchdown, and that all-important fourth-down conversion.
San Francisco's offense was led by running back Raheem Mostert, who gained 146 yards on 19 carries, including a 40-yard touchdown scamper. Jimmy Garoppolo went 15 of 21 for 165 yards and one touchdown with no interceptions.
With the win, the Ravens moved to 10-2, while the Niners fell to the same record with division rival Seattle Seahawks (9-2) facing Minnesota on Monday night.
In a thrilling first half, the two teams exchanged touchdowns despite the expectation of a defensive battle.
On the very first drive of the game, the 49ers took the lead through a fantastic, leaping 33-yard touchdown grab from Deebo Samuel as he rose above Marcus Peters to secure the catch.
The play came on a fourth down as San Francisco opted to be aggressive early.
After the Ravens were forced to punt for the first time since Week Nine under Jackson (21 drives), their defense made a play to get back in it.
Under intense pressure, Chuck Clark stripped the ball from Garoppolo and Brandon Williams recovered at San Francisco's 23-yard line.
Just two plays later, the Ravens capitalised as Jackson floated a high pass to tight end Mark Andrews and he came down with it from 20 yards out.
When they got it again after a 49ers punt, Baltimore took the lead. With a 13-play, 65-yard drive, Jackson took his team down the field with a combination of passing and rushing, capping off the drive with a one-yard touchdown run.
Jackson faked the hand-off, the Niners' defense lost contain, and the quarterback strolled in untouched to make it 14-7.
In response, San Francisco raced down the field with a speedy five-play, 75-yard drive. Raheem Mostert's 40-yard TD run was created by fantastic blocking but allowed to happen by poor tackling by Peters and Ravens safety Earl Thomas.
After Justin Tucker had tacked on a field goal to give Baltimore their lead back at 17-14, San Francisco drove into range before the half but Robbie Gould's 51-yard attempt sailed wide to the right.
To start the second, the Ravens drove into opposition territory but were halted when Jackson lost his first fumble of the season.
With a fantastic effort, 49ers safety Marcell Harris chased down Jackson, and stripped the ball right out of his hands and into his own possession. The turnover allowed Gould to tie the game at 17-17 after Garoppolo and company drove down the field and took eight minutes off the clock.
A Baltimore punt pinned the 49ers at their own one-yard line, and that gave them solid field position as they got the ball back, starting at their own 41.
Continuing with their fourth-down aggressiveness all season, John Harbaugh elected to go for it from San Francisco's 40 early in the fourth quarter, but Jackson's pass to Andrews fell short.
In turn, the 49ers went for it themselves at Baltimore's 35-yard line. But again, the defense won the battle, as Garoppolo's fourth-and-one pass was tipped at the line and hit the turf with just over six minutes to go.
When Baltimore got the ball back, they didn't let it go. This time from their own 44-yard line, Jackson took the ball himself on fourth and one and converted with a three-yard rush, giving the Ravens a fresh set of downs and the chance to get into field goal range for Tucker.
With a methodical, 12-play drive, the Ravens reached the 31-yard line and allowed the clock to run down to three seconds. Despite the rainy, muddy conditions, Pro Bowl kicker Tucker nailed the 49-yarder to seal the win.
Up next, the Ravens travel to the 9-3 Buffalo Bills, while the 49ers visit the 10-2 Saints.