Manchester United have announced that goalkeeper David de Gea has signed a new four-year contract with an option for a further year.
The 24-year-old Spain international was on the brink of joining Real Madrid last week but the deal collapsed when the required paperwork did not go through in time.
And, in an unexpected development, United have now agreed a new deal with De Gea which will keep him at Old Trafford until at least 2019, his previous contract having been due to run out at the end of the season.
"I am delighted to be starting this new chapter in my United career," said De Gea.
"I have always enjoyed playing with these great players in front of our fantastic fans. Manchester United is a special club and Old Trafford is an ideal place for me to continue to develop my career.
"I'm looking forward to putting a difficult summer behind me and concentrating on working hard to improve and help my team-mates to be successful."
United boss Louis van Gaal said: "I am absolutely delighted David has signed a new contract, he is one of the best goalkeepers in world football.
"I am very pleased that he will be part of the team for many years to come. David made a very important contribution to our performance last season and he has been the club's player of the year for the last two seasons.
"He is a popular player who is keen to learn and enhance his game. At such a young age for a goalkeeper, he has many years ahead of him."
I am very pleased that he will be part of the team for many years to come.
De Gea joined United from Atletico Madrid in the summer of 2011 and, after a somewhat uncertain start, has emerged as one of the best in the world, winning back-to-back player of the year awards at United over the past two seasons.
However, he has yet to play for the club this season because Van Gaal felt he was lacking in focus due to Real Madrid's much-publicised interest in signing him.
Following the collapse of his planned move to Real, the Spanish club and United issued statements blaming each other for the failure to get the deal done.
De Gea then played his first match of the season in Macedonia on Tuesday night as Spain triumphed 1-0 in a Euro 2016 qualifier.
Speculation that he was set to carry out a u-turn and commit his future to Manchester United mounted on Thursday when his agent Jorge Mendes and members of his family were spotted going into the club's training ground.
And De Gea could complete a remarkable turnaround by lining up for Saturday's Premier League match against United's bitter rivals Liverpool at Old Trafford.