Ruiz recorded a monster upset, by knocking down Britain's Joshua four times to take the World Boxing Association, International Boxing Federation and World Boxing Organization titles.
The 29-year-old Ruiz came into the fight as a massive 32-1 underdog. But he won it by TKO in the middle of the seventh when the referee stopped it at 1:26 of the round after the fourth knockdown.
Ruiz, sent to the canvas once himself, knocked Joshua down twice in the third round and twice more in the seventh.
"I still can't believe it. I am still pinching myself to see if this is real," Ruiz said.
Joshua (22-1, 21 KOs) had a massive height and reach advantage as he weighed in at a chiselled 247 pounds (112.4 kilos) alongside the portly Ruiz, who beefed up to 268 pounds compared to 250 for his most recent fight.
Ruiz, who has won four fights in a row, looked fearless and poised while Joshua -- who delivered 21 knockouts in his 22 victories -- never seemed to recover from the first knockdown early in the third.
"Riuz won fair and square but I know Anthony Joshua's work ethic. Great fighters come back and improve," said Joshua's promoter Eddie Hearn.
Ruiz (33-1, 22 KOs) has been underestimated because of his physique, but he knew what was at stake and how fortunate he was to get a second chance at a world title.
He was a late replacement for Jarrell Miller, who withdrew from the bout with Joshua after failing three drug tests in April.
"Everything is possible man. Look at me, I just made history," Ruiz said.
Joshua was knocked down for the first time in the third when Ruiz hit him with a left hook to the temple. Realizing he had the champ in trouble, Ruiz then charged forward and got the second knockdown by connecting on a straight right.
Joshua got up at the count of eight but it was late in the round and he was saved by the bell.
"The speed got him," said Ruiz.
Ruiz was the first to go to the mat when he got hit by a left hook from Joshua earlier in the third.
Joshua got up after the third knockdown and fourth knockdowns in the seventh but he spat out his mouthguard and the referee waived it off while he leaned on the ropes.
Joshua needed medical attention after the bout and did not attend the post fight news conference.
Joshua, who won a gold medal at the 2012 Olympics, was coming off a nine-month layoff since knocking out Alexander Povetkin in September.
Ruiz's ambush win is being hailed by some as one of the major upsets in heavyweight history, ranking with 42-1 underdog Buster Douglas knocking out Mike Tyson in 1990 and Leon Spinks' 15-round split decision over Muhammad Ali in 1978.
"We did an upset, just exactly what I have been saying from all the people that doubted me, I just proved them wrong," Ruiz said.
Ruiz, who was born in San Diego and carries a US passport, considers himself Mexican and is now the first heavyweight champion of Mexican descent.
"I think Mexico has a champ in every weight and now we have a heavyweight champion. I am so happy," said Ruiz.
The loss will be devastating for the career of Joshua who was looking past Ruiz for a superfight unification showdown with WBC champion Deontay Wilder.
Together with Briton Tyson Fury they are trying to revive the sport's prestige division but the inability to get them together in the ring had left a sour taste in the mouth of boxing fans.
One of the first to pile on Joshua was Wilder who tweeted, "He wasn't a true champion. His whole career was consistent of lies, contradiction and gifts .... now we know who was running from who!!!!"
Fury was less critical, tweeting "We have our back and Forth's but @anthonyfjoshua changed his stars through life, heavyweight boxing. These things happen, rest up, recover, regroup and come again."