Last summer Hamilton signed a two-year contract extension at Mercedes covering the 2024 and 2025 seasons, but he will now join Ferrari one season into that deal, partnering Charles Leclerc at the Scuderia from next year.
Hamilton's move means Leclerc's current partner Carlos Sainz will drive elsewhere in 2025 after the expiry of his current terms, with Leclerc recently committing to a long-term deal at Maranello.
Ferrari confirmed the move with a short statement following Mercedes's.
"Scuderia Ferrari is pleased to announce that Lewis Hamilton will be joining the team in 2025, on a multi-year contract," it said.
The news is set to bring an end to what will be a 12-year stay for Hamilton at Mercedes, having joined Brackley in 2013 in a similarly spectacular move from McLaren, the team with which he made his debut in 2007 and won his first world title the following year.
That move proved a huge success, with Mercedes becoming the dominant force at the dawn of the turbo hybrid era in 2014. The team propelled Hamilton to six further world titles in seven years, only losing out to team-mate Nico Rosberg in 2016.
Hamilton amassed 82 of his 103 wins and 78 of his 104 poles for the squad, etching his status as the most successful driver of all time.
Hamilton's throne was ultimately usurped by Max Verstappen in a controversial 2021 title showdown, and subsequently, Mercedes lost its top spot to Red Bull, with Hamilton winless since the 2022-era regulations came into force.
Aged 39, Hamilton has now opted for a late change of scenery in the autumn of his career, and a reunion with Ferrari team principal Fred Vasseur, who was the Briton's team boss at ART in GP2.
As Ferrari re-signed Leclerc for 2025 and beyond, contract talks with Sainz were not progressing as swiftly, with Hamilton's availability now emerging as the underlying reason.
His arrival is now set to bring Sainz's four-year spell in Maranello to an end, having taken his first two grand prix wins with the squad.
The 29-year-old also claimed five poles and 16 further podiums for the team after earlier stints at McLaren, Renault and Toro Rosso.
Shortly after the announcement of Hamilton's move, Sainz issued a response confirming his Ferrari exit.
"Following today's news, Scuderia Ferrari and myself will part ways at the end of 2024," Sainz wrote on his social media channels.
"We still have a long season ahead of us and, like always, I will give my absolute best for the team and for the tifosi all around the world.
"News about my future will be announced in due course."
Hamilton's surprise departure for pastures new leaves a hole in Mercedes' driver line-up for 2025 alongside George Russell, just as the likes of Leclerc and McLaren's Lando Norris have been taken off the board by their respective teams.
As well as a straight swap with Sainz, Williams' Alex Albon and Alpine's Esteban Ocon could be among the options Mercedes will weigh up in the coming weeks and months.