Manchester United had been tracking the attacking midfielder, 21.
Boss Alex Ferguson went to watch him when Werder Bremen played a friendly at Fulham earlier this month.
But Jose Mourinho got his man after Bremen turned down an initial offer from the Spanish giants on Friday.
Ozil, who would have been available as a free agent next summer, joins compatriot Sami Khedira at the Bernabeu along with fellow new-boys Angel Di Maria and Ricardo Carvalho.
A Madrid statement said: "Ozil lands at Real after a standout performance at the 2010 World Cup in South Africa.
"His unique touch, un- matched technique, excellent ball control and deadly last pass make him one of the brightest up-and-coming stars in European football."
Bremen's director of sport Klaus Allofs said: "We are respecting the player's wishes to take the next step in his career immediately."
Ozil, born in Gelsenkirchen to Turkish parents, scored 12 goals in 71 appearances for Bremen.
But he spent 10 years doing his best to avoid the Manchester United boss.
Giggs admits his relationship with Fergie has changed over the last two decades.
The Welsh wizard revealed: "My relationship with him is different.
"When I first met him, I was 13. For the next 10 years you try to keep out of the way.
"That's particularly the case when you've had a bad game.
"But he asks your advice more now than when you were a younger player.
"You have that experience, you're in the changing room day in, day out."
Giggs is starting his 21st season at United and maintained his record of scoring in each of them by grabbing the third goal in Monday's 3-0 win over Newcastle.
He is as hungry for success as ever and knows his manager is just the same, even at 68.
Giggs added: "Don't go thinking the desire and the hunger are no longer there.
"He still wants to win and still hates losing."
Hernandez left the South Americans to join Manchester United before the World Cup and immediately impressed during pre-season.
The 22-year-old scored in the Community Shield win over Chelsea and made a late cameo in United's 3-0 victory over Newcastle in the Premier League on Monday.
But the Mexican's move to Old Trafford means he has missed out on the chance to scoop Latin American football's top club prize.
Chivas face Brazilian outfit Internacional in the second leg of the Copa Libertadores final on Thursday and the Mexican side have a mountain to climb after losing the first leg 2-1.
Vergara said: "It won't be easy, but the team are convinced they can turn it around."
Internacional midfielder Sandro signed for Tottenham earlier this year, but the Brazilians insisted the player remained until the end of their Libertadores campaign.
And Vergara now wishes he had negotiated a similar deal for Hernandez.
He added: "I am certain he would have made an important contribution against Internacional.
"Maybe our mistake was not having kept Javier at Chivas until the final."
The deal puts an end to the transfer speculation surrounding the Serbian, with stories claiming his future was destined to be away from Old Trafford.
Both AC Milan and Real Madrid were said to be showing an interest in the 28-year-old after the World Cup this summer.
However, United chief exec David Gill had recently confirmed that Vidic had agreed outline terms on a new contract.
The finishing touches have now been made, leaving Ferguson glad to have Vidic around until 2014.
He said: "We are delighted Nemanja has committed his future to the club.
"This kills all the silly speculation over the past few months.
"Nemanja has developed into one of the best centre-halves in Europe and we are pleased he will be here for many years to come."
With Rio Ferdinand and Gary Neville missing and Ryan Giggs starting on the substitutes' bench, Vidic has captained United this season and led the Old Trafford team to their Community Shield triumph over Chelsea at Wembley earlier this month.
He said: "I am very happy to sign this contract.
"I came to this club when I was young and have developed enormously with the help of the manager, coaching staff and my team-mates.
"During my time here we have had a lot of success and I look forward to continuing that success in the coming seasons."
New Premier League rules say all bosses must talk to the Beeb after games or will be fined, with the scale of punishments increasing for repeat offenders.
The first fine is £1,000 but this rises each time with the cost hitting around £60,000 for the full season.
Fergie has not spoken to the BBC since a 2004 documentary about his son Jason's work as an agent.
Match of the Day pundit Alan Shearer said: "I don't think he will speak to us again.
"Alex is a man of his word and a man of principle."
The Manchester United boss snubbed the BBC after yesterday's 2-2 draw at Fulham, continuing the silent treatment he first imposed in 2004 when the corporation made allegations against his son Jason in a TV documentary.
From this season, Ferguson can be fined by the Premier League for not speaking with the TV crew, but it hasn't cooled his stance.
MotD pundit Shearer said: "Alex is a man of his word and a man of principle.
"I don't think he will speak to us again."
United have said they are backing their manager, which will leave the club open to charges.
Any possible sanctions will be discussed by chief executive Richard Scudamore, chairman Sir David Richards and secretary Mike Foster at a Premier League board meeting at the end of next month.
The suggestion is that a series of fines will be imposed, starting at £1,000 and increasing with each subsequent snub to the Beeb.
Over a 38-game season the fines could total in the region of £60,000 but actually make no difference to Ferguson's stance.
Ibrox fans rioted after their team lost 2-0 to Zenit St Petersburg in the 2008 UEFA Cup final at Eastlands.
And police chiefs, who blocked plans for United defender Gary Neville to stage a testimonial match against the SPL champions earlier this year, are now on high alert for the September 14 Old Trafford clash.
A UEFA spokesman revealed plans are in place to avoid trouble.
He said: "On top of the usual delegates, we'll send security officers to both clubs to assess the situation.
"They'll speak to the clubs and police and see how fans will be met at airports and stations - and where they'll stay."
United chief executive David Gill saw no reason for panic.
He said: "I don't think it's something we need to worry about. Clearly we have to work with the authorities and Rangers.
"But they will not be bringing the number of fans who came to the UEFA Cup final.
"Boss Alex Ferguson is delighted to be drawn with Rangers. It should be a great occasion."
United played the Gers twice before in the Champions League in 2003 - and both games passed without any major trouble.
And Gill's opposite number at Rangers, Martin Bain, said: "When we went to Manchester for the UEFA Cup final it was one of the biggest movements of people in Europe with 150,000 descending on the city.
"Going down in those vast numbers was a lot for any club and city to deal with. It was a final.
"It was a minority who spoiled it for the majority but this is a different tie.
"The true Rangers fans will go to Manchester to prove they're a great set of supporters."
Rangers boss Walter Smith was briefly Fergie's No2 at Old Trafford and Gers boss when they beat Leeds in the original Battle of Britain clash - a 1992 Champions League qualifier.
He said: "It's a great draw. To be up against Sir Alex in my last season in charge is something I'll relish."