"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 lineup 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. He raced for Ferrari from 2007-09, and his title in 2007 was the last time Ferrari won the driver's championship.
"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.''
Alonso and Raikkonen have won three Formula One titles and 52 races between them.
"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.''
Ferrari apparently wanted to strengthen its lineup for 2014, when F1 regulations change dramatically with the introduction of new 1.6-liter V6 turbo engines.
Massa won 11 races in his first three seasons with Ferrari but 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.''
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.
"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.''