Hamilton, 36, has won six of his seven F1 titles with the outfit since joining them in 2013.
The Briton is a prominent advocate against racism and took the knee wearing a Black Lives Matter t-shirt before each race last season.
"It is hard to believe it's been nearly nine years working with this incredible team and I'm excited we're going to continue our partnership for two more years," Hamilton said in a statement.
"We've accomplished so much together but we still have a lot to achieve, both on and off the track. I'm incredibly proud and grateful of how Mercedes has supported me in my drive to improve diversity and equality in our sport.
"They have held themselves accountable and made important strides in creating a more diverse team and inclusive environment."
Hamilton is one championship victory away from overtaking the all-time record he shares with Michael Schumacher but has more race wins than the Ferrari icon.
He has won three of this season's eight races so far but trails championship leader Max Verstappen by 18 points.
On Friday he outpaced Red Bull's Verstappen in practice for this weekend's Austrian Grand Prix.
"His achievements in this sport speak for themselves, and with his experience, speed and race-craft, he is at the peak of his powers," Mercedes team principal and chief executive Toto Wolff said.
"We are relishing the battle we have on our hands this year -- and that's why we also wanted to agree this contract early, so we have no distractions from the competition on track.
"I have always said that as long as Lewis still possesses the fire for racing, he can continue as long as he wants."
Next season, Formula 1 will introduce alterations to the cars, which were pushed back from this year due to the coronavirus pandemic.
They aim to aid drivers to overtake more often.
"As F1 enters a new era in 2022 with wholesale technical regulation changes, Mercedes and Lewis will forge new ground in a collaboration which has spanned his entire F1 career," Mercedes said.
On Sunday in Spielberg Hamilton will look to stop Verstappen from claiming a third consecutive win. The Dutch driver started his last two victories from pole position.
After this weekend's racing, the season takes a break before the British Grand Prix on July 18, the 10th of a 23-race campaign.