Canada's Raonic, the fifth seed, struggled physically as American qualifier Ryan Harrison - the world number 120 - won 6-7 (4-7) 7-5 7-5 6-1.
Spanish third seed Muguruza lost 7-5 6-4 to Latvia's Anastasija Sevastova.
Top seed Novak Djokovic reached round three without hitting a ball as Jiri Vesely withdrew before their match.
The Czech, 23, said he was "very, very disappointed" to pull out with inflammation of his left forearm.
Defending champion Djokovic, who has been struggling with a wrist injury, now faces Russia's Mikhail Youzhny on Friday.
Rafael Nadal safely navigated the challenge of Andreas Seppi, beating the Italian 6-0 7-5 6-1 under the roof on Arthur Ashe Stadium for the first time.
Former champion Marin Cilic and French seeds Gael Monfils and Jo-Wilfried Tsonga also progressed.
Raonic was among the favourites for the title after reaching the semi-finals at the Australian Open and the final at Wimbledon, losing to Andy Murray on both occasions.
However, the 25-year-old struggled to carry his bags by the time he left the court following defeat by Harrison.
"It was probably just nerves and stress, a mental sort of over-exuberance," Raonic said.
"Cramping in the left arm, right forearm there towards the end of the third set, both quads, a little bit in the hip flexor on the left. It was just catching me all over.
"I couldn't switch grips from one point to the next. There were a few points where I would hold the racquet with my left hand and trying to stretch out my right hand in between shots."
Muguruza, making her debut on Arthur Ashe Stadium, got the first break of serve before being overhauled by Sevastova, ranked 48th in the world.
The Spaniard, 22, fought back from facing two match points at 5-1 down in the second set to get back on serve at 5-4, only for Sevastova to claim a seventh break of the night.
"I was shaking a little bit in the end but it's amazing, said the Latvian, who retired in 2013 because of injuries and illness before returning in 2015.
Muguruza was surprised to suffer her second successive defeat in round two of the US Open.
"It's very rare when you play your best," said the French Open champion. "Everything I was trying to do, she was coming back and doing incredible shots."
Nadal, champion in New York in 2010 and 2013, became the first man to hit under the new retractable roof that has been fitted on the court.
When rain started in the second set, there was a brief suspension, but play restarted within eight minutes.
"It's just an unbelievable improvement," Nadal said. "I feel that the conditions are pretty similar when the roof is closed or open."
Two-time finalist Caroline Wozniacki fought back superbly to beat ninth seed and former champion Svetlana Kuznetsova.
Wozniacki, a former world number one, has slipped to 74th in the rankings and was one point from falling 5-0 behind against Kuznetsova.
However, the Dane battled back to win 6-4 6-4 and reach the third round at a Grand Slam for the first time since Wimbledon last year.
"I stopped looking at the ranking once I fell out of the top 10," the 26-year-old said. "I feel like I belong at the top."
Germany's Angelique Kerber, seeded second and in with a chance of taking the number one ranking, beat Mirjana Lucic-Baroni of Croatia 6-2 7-6 (9-7).
Kerber will now face 17-year-old qualifier Cici Bellis, who beat fellow American Shelby Rogers 2-6 6-2 6-2.
Roberta Vinci, Dominika Cibulkova, Johanna Konta and Petra Kvitova were other seeds to progress.
Fourth seeds Jamie Murray and Brazilian Bruno Soares came through a tight opening match against Gastao Elias and Joao Sousa, seeing off the Portuguese pair 6-3 6-7 (3-7) 7-5.
They were joined in the last 32 by the pairing of Britain's Dan Evans and Australian Nick Kyrgios, who defeated Americans Daniel Nguyen and Noah Rubin 6-3 6-3.
French Open champion Garbine Muguruza and Wimbledon finalist Milos Raonic made shock exits in the second round of the US Open.