BARCELONA, SPAIN (AP) - Lionel Messi will play out the best remaining years of his trophy-rich career at Barcelona.

The Spanish club said Wednesday that Messi had agreed a three-year contract extension that will keep him at the club through the 2020-21 season. By then, Barcelona's all-time leading scorer will be 34 years old and finishing his 17th season with the club.

Barcelona announced that Messi will sign the new deal "in the coming weeks" that will be good through June 30, 2021.

The contract extension comes as no surprise. Messi has repeatedly said he wanted to play the best years of his career at Barcelona, before perhaps making a return to his native Argentina that he left at 13 years old to come to Barcelona's famed La Masia training academy.

But with his current contract set to expire at the end of the upcoming 2017-18 season, the club's board, players, fans and new coach Ernesto Valverde can all breathe easier this summer knowing their star is staying put.

Barcelona released a statement in English calling Messi "the best player in history, who has played his full professional career at Barca and has led the team to an era of extraordinary success, the likes of which has never been seen in world football."

Since making his debut with Barcelona's first team at age 17 in 2004, Messi has established himself as one of the best to ever play the game. He has won the FIFA world player of the year five times, and has become the Spanish league's all-time leading scorer.

Messi, who turned 30 last month, has scored a club record 507 goals in 583 matches. His honors with the club include four Champions League trophies, eight Spanish league titles and five Copa del Rey titles.

The announcement comes five days after Messi married his childhood sweetheart Antonella Roccuzzo, mother of their two sons.

Messi will now be expected to again lead a Barcelona side that has been eclipsed for the first time in more than a decade by fierce rival Real Madrid, which won both the Champions League and La Liga titles last season.