The 25-year-old Madrid youth product -- who scored six goals in 18 appearances on loan at Getafe last season -- has agreed a five-year contract and will be presented by his new club on Thursday.
"Borja Mayoral arrived at our academy in the year 2007 and has been a part of Real Madrid since that time, winning a Champions League, a Club World Cup, a UEFA Super Cup and a Spanish Supercopa," Real Madrid said in a statement. "Real Madrid wishes him and his family all the best in this new stage in their life."
A series of loan moves followed, first to Wolfsburg in the Bundesliga and then to Levante in LaLiga, before an eye-catching two-year spell at Roma in 2020.
The Spain youth international impressed in his first season in Serie A, scoring 10 goals, but fell out of favour with the arrival of Jose Mourinho as manager.
He cut short his time in Rome to join Getafe on loan in January this year and made an instant impact, scoring six times to help them secure survival in LaLiga and an eventual 15th-placed finish under coach Quique Sanchez Flores.
Mayoral was an unused substitute in Real Madrid's 2-0 win over Juventus in Los Angeles on Saturday, the final game of their tour of the United States.