The bombshell news, confirmed first by Mercedes and then in a one-line statement from Ferrari, came after a day of mounting speculation with the world awaiting news from Maranello and Brackley.
"Scuderia Ferrari is pleased to announce that Lewis Hamilton will be joining the team in 2025, on a multi-year contract," Ferrari said.
"I have had an amazing 11 years with this team and I'm so proud of what we have achieved together. Mercedes has been part of my life since I was 13 years old," said Hamilton in the Mercedes statement.
"It's a place where I have grown up, so making the decision to leave was one of the hardest decisions I have ever had to make.
"But the time is right for me to take this step and I'm excited to be taking on a new challenge."
Hamilton said he wanted to finish his time at Mercedes on a high and was "100% committed to delivering the best performance I can this season and making my last year with the Silver Arrows, one to remember".
The 39-year-old Briton is set to replace Spanish driver Carlos Sainz at Ferrari alongside Charles Leclerc and gives the Italian team a world champion in one of their cars for the first time since Germany's Sebastian Vettel left in 2020.
The biggest driver move since Hamilton left a winning McLaren for then-unproven Mercedes at the end of 2012 came as a surprise despite regular chatter that it could one day happen.
Ferrari are the oldest, most glamorous and successful team but without a driver's title since 2007 and like Hamilton hungry to get back to the top. The Briton has not won a race since December, 2021.
His seven crowns are matched only by German Michael Schumacher, who raced for Ferrari from 1996 to 2006 during a golden era at Maranello and made a comeback at Mercedes.
Hamilton's switch raised plenty of questions about why he had decided to leave, a month before the start of the season, but it extends his career into the sport's new engine era in 2026 and keeps him firmly in the limelight.
Despite his loyalty to Mercedes, Hamilton has remained close to Ferrari boss Fred Vasseur after winning Formula Three and GP2 (now Formula Two) championships with the Frenchman's ART team as a junior in 2005 and 2006.
He will leave Mercedes with a long list of records.
"In terms of a team-driver pairing, our relationship with Lewis has become the most successful the sport has seen, and that's something we can look back on with pride," said Mercedes team boss Toto Wolff.
"Lewis will always be an important part of Mercedes motorsport history.
"However, we knew our partnership would come to a natural end at some point, and that day has now come. We accept Lewis's decision to seek a fresh challenge, and our opportunities for the future are exciting to contemplate."
Winner of a record 103 races and with 104 pole positions in a career that started in 2007, Hamilton has not had the car to challenge Red Bull and Max Verstappen in recent seasons.
The Briton was third overall last year while Mercedes were runners-up.
Hamilton will have a well-established and quick team mate in Leclerc, whose contract was renewed last week for several more seasons.
Mercedes must now start the search for a partner to Britain's George Russell, with Sainz one possible candidate although the Spaniard has also been linked to Audi when Sauber become their works team in 2026.