A Super Bowl-record 108-yard kick-off return for a touchdown by Jacoby Jones to open the second half gave the Ravens a 28-6 but the 49ers scored 17 straight points after a 35-minute power outage and nearly pulled off a stunning comeback in New Orleans.
"Both teams had to deal with it," Ravens coach John Harbaugh said of the power outage. "They dealt with it better, obviously. They were able to turn the momentum of the game."
Trailing 34-29, the 49ers marched down to the Ravens' seven but failed to score on four tries, surrendering the ball on downs at the five with less than two minutes remaining.
Baltimore used up the clock and on fourth down, punter Sam Koch ran out of bounds in the end zone for a safety with four seconds left.
Joe Flacco, the game's Most Valuable Player, completed 22 of 33 passes for 287 yards, three touchdowns and no interceptions to lead the Ravens to their first title since the 2000 season.
"We don't make anything easy," said Flacco, who has guided the Ravens to the play-offs in each of his five NFL seasons. "It was a hard-fought game on both sides.
"We gave the country a pretty good game to watch. Not to our liking necessarily but that's the way it goes sometimes and that's the way we do things."
Baltimore's lead evaporated because the 49ers' talented second-year quarterback, Colin Kaepernick, suddenly found the form that made him one of the league's most dangerous quarterbacks this season.
Despite a shaky first half, Kaepernick completed 16 of 28 passes for 302 yards and a touchdown. He also ran for 62 yards and a 15-yard touchdown on seven carries to finish behind Frank Gore (110 yards) as the 49ers' second-leading ground gainer.
Kaepernick's rushing score with 2:57 left cut the Baltimore lead to 31-29 but his pass attempt on the two-point conversion attempt was incomplete.
A 38-yard field goal by Justin Tucker with 4:19 left in the game hiked the Ravens' lead to 34-29, setting the stage for what could have been a Super Bowl-record comeback by the 49ers.
But the Ravens' defense, anchored by retiring future Hall of Fame linebacker Ray Lewis, came up big when it had to with a goal-line stand and handed the 49ers' franchise its first Super Bowl loss after five victories.
Baltimore linebacker Terrell Suggs said it was the "ultimate cake" to allow Lewis to go out a winner.
"There will never be another leader like him and we sent him out like his brothers," he said. "His legacy will go untainted."
Ravens tight end Dennis Pitta, who caught four passes for 26 yards and a touchdown, said the power outage, which left half of the overheard lights in the Superdome out, "felt like an hour."
"We lost a lot of momentum and, credit to them, they came storming back and played tremendous. But this team continued to fight and never quit ... Didn't make it easy, but it was fun."
Lewis said his children will be his top priority in retirement.
"Daddy gets to come home now," he said. "I get to chase them. They aren't going to like me being home all the time. This is the most ultimate feeling ever.
"This is the way you do it. No other way to go out and end a career."
SF 0-7 BAL (10:36) - BALTIMORE TD: Anquan Boldin 13-yard pass from Joe Flacco (Justin Tucker extra point). Drive: 6 plays, 51 yards in 2:29.
SF 3-7 BAL (3:58) - SAN FRANCISCO FG: David Akers 36-yard field goal. Drive: 12 plays, 62 yards in 6:33.
SF 3-14 BAL (7:10) - BALTIMORE TD: Dennis Pitta 1-yard pass from Joe Flacco (Justin Tucker extra point). Drive: 10 plays, 75 yards in 4:38.
SF 3-21 BAL (1:45) - BALTIMORE TD: Jacoby Jones 56-yard pass from Joe Flacco (Justin Tucker extra point). Drive: 3 plays, 56 yards in 0:22.
SF 6-21 BAL (0:00) - SAN FRANCISCO FG: David Akers 27-yard field goal. Drive: 8 plays, 71 yards in 1:45.
SF 6-28 BAL (14:49) - BALTIMORE TD: Jacoby Jones 108-yard kick-off return. Drive: 0 plays, 0 yards in 0:11.
SF 13-28 BAL (7:20) - SAN FRANCISCO TD: Michael Crabtree 31-yard pass from Colin Kaepernick (David Akers extra point). Drive: 7 plays, 80 yards in 2:58.
SF 20-28 BAL (4:59) - SAN FRANCISCO TD: Frank Gore 6-yard run (David Akers extra point). Drive: 2 plays, 20 yards in 0:43.
SF 23-28 BAL (3:10) - SAN FRANCISCO FG: David Akers 34-yard field goal. Drive: 4 plays, 8 yards in 1:00.
SF 23-31 BAL (12:54) - BALTIMORE FG: Justin Tucker 19-yard field goal. Drive: 12 plays, 71 yards in 5:16.
SF 29-31 BAL (9:57) - SAN FRANCISCO TD: Colin Kaepernick 15-yard run (Two point conversion failed). Drive: 5 plays, 76 yards in 2:57.
SF 29-34 BAL (4:19) - BALTIMORE FG: Justin Tucker 38-yard field goal. Drive: 10 plays, 59 yards in 5:38.
SF 31-34 BAL (0:04) - SAN FRANCISCO SAFETY: Sam Koch goes out of bounds in end zone.