Real Madrid have signed the midfielder Asier Illarramendi from Real Sociedad after agreeing to meet the Spain Under-21 international's buy-out clause, reported to be €39m (£33.6m).

The two clubs had been in negotiations regarding a fee for a player Madrid believe to be the long-term replacement for Xabi Alonso in a defensive midfield role, but had failed to find a resolution. Sociedad announced on Friday that Madrid had informed the Spanish Football League they would pay the clause.

Meanwhile, a statement from Madrid said: "Real Madrid have signed Asier Illarramendi, who is now tied to the club for the next six seasons. The player will be presented tomorrow after undergoing the relevant medical tests."

Illarramendi rose to prominence as a key man at Sociedad last season, playing an important role as the club finished fourth in La Liga, securing a place in the Champions League in the process. The 23-year-old then played every game as Spain Under-21s won the European Championship in Israel earlier this summer.

He becomes Madrid's fourth signing of the summer, after fellow Spaniards Isco and Dani Carvajal joined from Málaga and Bayer Leverkusen respectively, and the club made their loan move for the Brazilian midfielder Casemiro permanent.

The Sociedad president, Jokin Aperribay, admitted they did not want to sell the player. "This morning the LFP called to tell us that Real Madrid have activated Illarramendi's buyout clause," he said. "We have a very important year ahead of us with a young coach [Jagoba Arrasate] and we wanted all our players to stay.

"We told Real Madrid that we wouldn't negotiate, and we said the same thing to Asier and his agents. We fought very hard for him to stay and it's been a difficult few days because we really wanted him to stay. We wish him all the best and hope he doesn't forget his time at La Real."

Illarramendi, who has spent his entire youth and professional career at Sociedad, explained he was leaving the Basque side with a heavy heart, but admitted he could not turn down the chance to join Real Madrid.

"It hasn't been an easy decision, it has been very difficult for me to decide because Real Sociedad have given me so much," he said. "I was on holiday when we started to hear about the interest from Real Madrid. There were contacts, we mulled it over and came to the conclusion that it was an offer we couldn't refuse. It's a unique opportunity.

"I thank the board, the academy coaches, the backroom staff and my team-mates. I wish them all the best. This club will achieve great things."