The Ravens and their standout running back went right down to the 4 p.m. deadline before hammering out the deal. Had Rice not signed, he would have played the 2012 season under the $7.7 million franchise tag tender.
Instead, he will get $17 million this year and $8 million in 2013.
"Baltimore I'm back well I never left lol #Ravens," Rice wrote on Twitter on Monday.
Baltimore has made the playoffs in each of Rice's four seasons, twice advancing to the AFC title game. He earned his second Pro Bowl invite last year after leading the NFL with 2,068 yards from scrimmage.
"This is another example of (owner) Steve Bisciotti's commitment to the team and to our fans to retain our core players," Ravens general manager Ozzie Newsome said. "Ray has been an integral part of us earning the playoffs in each of his four seasons. His production on the field speaks for itself, and his leadership in the locker room is outstanding.
"I should say something about his community efforts; I think they are almost unmatched by any player in the NFL," Newsome said. "You'd have a hard time finding a player who does more or is as serious about helping others as Ray is. He is one of those players you can proudly say, 'He's on our team.' "
As Baltimore's franchise player, Rice would not have been required to report at the start of training camp. The Ravens will hold their first full squad workout July 26, and Rice is expected to attend.
"Interesting to read about some of the deals getting done today. Very happy for Ray Rice.," Hall of Fame running back Barry Sanders tweeted Monday.
Rice's contract expired after a season in which he amassed an NFL-best 2,068 yards from scrimmage and helped the Ravens advance to the AFC Championship Game.
Last September, Ravens defensive tackle Haloti Ngata traded his franchise tag for a five-year, $61 million contract. Rice wanted to follow a similar path.
Soon after Baltimore's loss to New England in the AFC title game, Rice said, "I'm not going to complain about the franchise tag, but I prefer the long-term. That's what Baltimore, the city, the fans, would love to know -- that Ray Rice is going to be here, not just one year, but for many years to come."
Rice scored a franchise-record 15 touchdowns in 2011 to go with a career-best 1,364 yards rushing. He also led Baltimore with 76 catches (for 704 yards).
Baltimore selected the 5-foot-8, 212-pounder from Rutgers in the second round of the 2008 draft. In 61 career games, he ranks second in franchise history in yards rushing (4,377) and yards from scrimmage (6,612).
Veteran running back Ricky Williams, who played behind Rice in 2011 and retired during the offseason, was impressed with Rice's talent and passion for the game.
"He's got all the God-given ability, but he also works hard," Williams said late last year. "It's rare to find that combination. In my mind, there's only one way to have success, and that's to work hard and have a little bit of talent. He's got both."
Information from The Associated Press contributed to this report.