No financial details were given in a Mercedes statement but British media reports have suggested the new deal could be worth up to £40 million a year and was likely to make Hamilton the sport's top earner.
The' announcement comes before the team's home race in Germany, with Hamilton trailing Sebastian Vettel in the championship by eight points.
Formula One champion Lewis Hamilton suffered a major setback to his German Grand Prix hopes on Saturday after stopping on track, and then trying to push his auto, during a dramatic qualifying session.
Unlike most drivers in F1, Hamilton negotiates his contracts himself and said it had helped build trust with Mercedes team boss Toto Wolff.
Ferrari appeared to step its game up a level at the British Grand Prix in Silverstone, with Vettel qualifying just 0.044s off Hamilton at a circuit where Mercedes has recently had the strongest vehicle.
Hot Springs duck tours set to continue charters hours after Branson tragedy
Nine people from one family are among the 17 who died when a duck boat sank on a Missouri lake, says the state governor's office. Thirteen people died in 1999 when the duck boat they were riding near Hot Springs, Arkansas, sank suddenly.
Vettel is due to stay at Ferrari for a further two years.
The spokesman said it was important to stop the auto to prevent "heat soak damage" to the power unit as happened to team-mate Bottas during the race in Austria earlier this month. The Red Bulls are very, very fast, both Red Bulls are really quick... "We have got a great relationship, and it has gone from strength to strength", said Hamilton.
"I said from day one 'I am not looking anywhere else?" While I know there are drivers calling you - which I expect - I also trust that you're not thinking to do anything else, so we can take our time'. I am not investigating to see what other options there are.
Hamilton's new contract goes to the end of the Concorde Agreement, with a new set of rules and regulations for F1 set for 2021 onward.
"I just kept delaying it", he added.
"Now we have to find out how we've been working compared to the others, but I think that even if we can still improve our lap time, the vehicle is fine". In total, he has 61 Formula One wins over the course of his career, second only to Michael Schumacher.