The four-time world champion would have been out of contract at the end of the season with negotiations becoming major talking point on the circuit.
No financial details were released by Mercedes but reports have suggested the new deal could be worth around £40m a year.
Hamilton’s current team-mate Valtteri Bottas is also out of contract at the end of the year but no update has been given by Mercedes over the Finn’s future.
Hamilton is currently eight points adrift of Ferrari’s Sebastian Vettel at the halfway stage of the season.
Vettel has won four races this year to his rival’s three.
“I can’t wait to see what we can achieve together in the next two-and-a-half seasons”
Next up is the German Grand Prix and Hamilton insists his desire for success is as strong as ever as he chases a fifth world title.
“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,” he said.
“I can’t wait to see what we can achieve together in the next two-and-a-half seasons.”
Mercedes chief Toto Wolff was aware there had been plenty of speculation around Hamilton in recent months and was happy to put it to bed.
“It’s good to put all of that to rest and get this thing announced,” he said.
“We signed the final documents this week and didn’t want to keep people waiting any longer.”
/ Motorsport – ENG Portals