A goal-line stand by the Ravens defense in the final two minutes stopped a furious comeback by the 49ers after a power outage in the Superdome and gave Ray Lewis a world championship in the final game of his career.

The defining defensive player of his generation needed one last-gasp stop to go out a champion.

The Ravens defense obliged.

In a goal-line stand that will go down as one of the greats in Super Bowl history, the Ravens held, forcing a fourth-down incompletion and escaping the Superdome with a 34-31 Super Bowl XLVII victory for the franchise’s second NFL title.

It was far from easy.

The Ravens nearly blew a 22-point lead, but never trailed. They had their momentum sapped by a freak blackout that stopped play for a half-hour early in the second half.

But they survived.

The Ravens survived because they rattled Colin Kaepernick after he had been so unflappable for so long. On fourth-and-goal from the 5 with the Niners needing a touchdown, Baltimore brought pressure up the middle, hurrying Kaepernick’s throw that never had a chance.

As the ball fell to earth, so did the Ravens defense, full of emotion after nearly blowing the biggest lead in NFL history.

“It’s simple,” Lewis said. “When God is for you, who can be against you? It’s no greater way as a champ, to go out on your last ride, than the men I went out with. Baltimore, Baltimore, we’re coming home baby!”

They got just enough offense early and defense late to survive four consecutive San Francisco scoring possessions.

On the final night of the Lewis’ sterling career, Joe Flacco was the best player on his team.

As for his Ravens? They’re the best football team on the planet.

Flacco was 22 of 33 for 287 yards and three touchdowns — all coming in the first half. He was named the game’s Most Valuable Player.

“We don’t make it easy,” Flacco said. “That’s the way the city of Baltimore is. That’s the way we are. We did this for them.”

Anquan Boldin went for 104 yards on six catches and had a score. The Ravens forced two first-half turnovers, including an interception by former University of Miami star Ed Reed.

They needed every bit of it.

They had just gone ahead 28-6 on a 108-yard kickoff return for a touchdown by Jacoby Jones — the longest play in Super Bowl history — when the power went out for 34 minutes, sapping all semblance of momentum.

When the lights came back on, Kaepernick lit it up. The Niners scored the game’s next 17 points — including a 31-yard pass from the second-year quarterback to Michael Crabtree and a 6-yard Frank Gore run — all in a span of 4 minutes 14 seconds.

Kaepernick made the Ravens sweat late, racing 15 yards to the end zone early in the fourth quarter. But he misfired on the two-point conversion, keeping the Ravens ahead.

Justin Tucker’s 38-yard field goal stretched Baltimore’s lead to five points and forced the Niners to need a touchdown to win. They didn’t. Baltimore took an intentional safety with just seconds remaining, and when the Ravens brought down former Dolphin Ted Ginn Jr. on the ensuing free kick, it was finally over.

In the battle of the Harbaughs, John bested little brother Jim.

On shaking hands with his brother, John said: “The hardest thing I’ve ever done. I told him I loved him.”

Said Jim Harbaugh: “We want to handle this with class and grace. We had several opportunities in the game. Didn’t play our best game.”

Still, Kaepernick alone was nearly enough. In just his 10th NFL start, he completed 16 of 28 passes for 302 yards and ran for 62 more on seven carries.

Crabtree and Vernon Davis both had more than 100 yards receiving, Coral Gables’ Gore rushed for 110 yards on 19 carries, and the Niners gashed Baltimore for 468 yards.

Yet they needed 5 more.

Despite having first-and-goal from the 7 and Kaepernick shredding the Ravens with his legs, the Niners didn’t have him scramble once.

And the Ravens defense, which carried the franchise to its first world championship in 2000, made one final stop for its leader.

The evening couldn’t have started better for the Ravens. After a defensive three-and-out to open the game, Baltimore took over near midfield. Six plays later, the Ravens were in the end zone.

Boldin, lining up in the slot, blew past linebacker NaVorro Bowman and hauled in 13-yard touchdown pass from Flacco, giving Baltimore an early 7-0 lead.

It was a lead the Ravens would never surrender.

Lewis is a two-time NFL Defensive Player of the Year and MVP of Super Bowl XXXV. But his skills in recent years have diminished, and he announced before the postseason that this would be his last ride.