The Spanish club announced this on Saturday, making official an agreement reached in July.
The contract of the soccer star with the Camp Nou side is supposed to expire in 2018.
“FC Barcelona and Lionel Messi signed on Saturday morning a new contract that will keep the Argentinian superstar at the club through the 2020/21 season,” the club said in a statement.
“The buyout clause was set at 700 million euros ($835 million).”
The new deal with the club comes with a €700m release fee.
He has played 602 times for the Catalans and has scored 523 goals, helping them to 30 trophies.
Among the trophies are La Liga (8 times), Copa del Rey crowns (five times) and four Champions League titles.
A goalscorer and creative playmaker, Messi takes credit for the most official goals scored in La Liga.
He made his debut for the Spanish team in 2004 when he was 17 years.