The 33-year-old had hinted earlier this year that he could leave the sport, but a reported £40m deal has convinced him to stay.
The contract makes him the highest paid British sportsperson.
In a statement released by Mercedes, Hamilton said:
"This contract extension has basically been a formality since Toto and I sat down during the winter, so it's good to put pen to paper, announce it and then get on with business as usual."
"I have been part of the Mercedes racing family for 20 years and I have never been happier inside a team than I am right now. We are on the same wavelength both on and off track - and I am looking forward to winning more in the future and shining even more light on the three-pointed star. I'm very confident that Mercedes is the right place to be over the coming years."
"Although we have enjoyed so much success together since 2013, Mercedes is hungrier than ever - from Dr Zetsche and the board members at the top of Daimler, through Toto and the team management, to every single person I meet in the corridors of Brixworth and Brackley. The competitive passion that burns bright inside me is shared by every single member of this group - always chasing the next improvement and digging even deeper to make sure we come out on top. I can't wait to see what we can achieve together in the next two-and-a-half seasons."
Lewis Hamilton (Mercedes) - GP of Great Britain 2018Getty Images
Hamilton's delay in signing the contract was reportedly due to his interest in expanding his music and fashion interests, but Mercedes executive director Toto Wolff was delighted to have kept his star driver in the team.
"We have been aligned with Lewis ever since we first sat down to discuss the details of this contract after last season, but there has understandably been a lot of interest and speculation around the whole process, so it's good to put all of that to rest and get this thing announced," Wolff said.
"We signed the final documents this week and didn't want to keep people waiting any longer! There is not much about Lewis as a Formula One driver that hasn't been said already - he is one of the all-time greats and his track record speaks for itself.
"But what I enjoy most about working with him is getting to know the man inside the racing helmet: his relentless drive for self-improvement, his emotional intelligence as a team member and his loyalty to those around him.
"Mercedes has become Lewis' home in Formula One and his story is linked forever with the silver and green of Mercedes-AMG Petronas. I am very confident that we have some incredible chapters of our story together still to come."
Radiuniverse Samsung S9 For Free
Source : https://www.eurosport.com/formula-1/lewis-hamilton-commits-future-to-mercedes-with-huge-40m-two-year-deal_sto6854013/story.shtml