"Scuderia Ferrari announces that it has reached an agreement with Kimi Raikkonen," the team said Wednesday in a statement. "The Finn will join Fernando Alonso in the driver line-up for the next two racing seasons."
The expected move came a day after Massa announced he will leave Ferrari following the seven final races of this season.
The 33-year-old Raikkonen, who currently drives for Lotus, had been heavily linked with a return to Ferrari. The Finn raced for Ferrari from 2007-09, and his title in 2007 was the last time Ferrari won the driver's championship.
"I'm really pleased to return to Maranello after having experienced three fantastic years there," Raikkonen said on Ferrari's website. "I have lots of memories that tie me to Ferrari, which I've always carried with me. I can't wait to work with Fernando, an extraordinary driver, to bring the team the success it deserves."
Alonso and Raikkonen have won three Formula One titles and 52 races between them.
Ferrari apparently wanted to strengthen its lineup for 2014, when the F1 regulations change dramatically with the introduction of new 1.6-litre V6 turbo engines.
Raikkonen has one win and five second-place finishes this season and is known as one of the most consistent drivers in the sport. His retirement in Belgium last month ended a run of 27 consecutive races in the points, and it was his first DNF in 39 races.
"I'm delighted to welcome Kimi back to the Scuderia with which he won the world title," Ferrari team principal Stefano Domenicali said. "He sounded extremely happy to return to Maranello and very determined to approach the job that awaits him in the best way possible."
Massa won 11 races in his first three seasons with Ferrari but he hasn't been the same driver since a life-threatening accident at the Hungarian Grand Prix in 2009. His contract is up at the end of this season.
"Felipe always carried himself as a real team man and together we lived through beautiful and dramatic moments, which created a really special human and professional relationship," Domenicali said. "He'll always be part of our family and we wish him well for his future."
In the current standings, Alonso is second to Red Bull driver Sebastian Vettel, Raikkonen is fourth and Massa is seventh.
Of Raikkonen's 20 career F1 wins, nine came during his three seasons with Ferrari. He won the 2007 championship, the only driver's title of his career, by one point ahead of Lewis Hamilton and Alonso, who was then with McLaren.
However, near the end of the 2009 season, Ferrari announced that Raikkonen was leaving the team even though his contract lasted through 2010. He was replaced by Alonso.
The announcement of Raikkonen's return comes during turbulent times at Ferrari, which this season has again been unable to stop Red Bull's dominance.
During last weekend's Italian GP, Alonso and Ferrari were again forced to downplay talk of a rift after the Spanish driver lost his temper with team officials during qualifying.
It remains to be seen if Alonso will also leave or duel for the team's top driver status with Raikkonen next season.
If Alonso does stay, it will mark the first time Ferrari has two former world champion drivers since Alain Prost and Nigel Mansell were at the wheels of the red cars in 1990.
"I want to welcome my new teammate," Alonso said on Ferrari's website. "Together we'll have to approach a difficult technical and sporting challenge starting next year."