Tournament officials said the former world number one was suffering from a viral illness and Williams later confirmed she had been feeling ill ahead of the third-round tie.
Williams consulted a trainer before testing herself in the second set, but subsequently pulled the plug on her afternoon after falling behind 1-0.
Williams left the court to warm applause, yet he ongoing fitness issues must be concerning the tennis legend who has struggled to hit top form since returning to the court following the birth of her first child a year ago.
"Before the match, I did not feel great, and then it just got worse with every second; extreme dizziness and extreme fatigue", Williams said in comments released via tournament organizers.
After the match, Muguruza sent her best wishes to Williams.
"I felt that maybe she wasn't feeling well", she said. "I will focus on getting better and start preparing for Miami", she said, referencing next week's Miami Open.More news: Apple Sets March 25 to Announce Its Next Big Thing
More news: Tesla to raise model prices by average of 3%
More news: Big WWE Raw Segment Announced For The Shield
Williams, who is now ranked number 10 in the world, has battled health issues in the past that nearly sidelined her ranking.
There were service breaks aplenty as Halep and Kozlova traded four on the way to the first-set tiebreaker and four more to open the second set.
Federer saved six of seven break chances against the German after losing just two points on his serve in the first set.
"It was hard for me to close out those sets", Halep told reporters, praising Kozlova.
Two seeded women lost: No. 11 Anastasija Sevastova, who retired trailing 5-0 in the first set against No. 21 Anett Kontaveit, and No. 15 Julia Goerges, who lost to Mona Barthel 7-5, 1-6, 6-4.
Venus Williams is turning back her own clock at the BNP Paribas Open, moving into the fourth round with a 6-2, 7-5 victory over qualifier Christina McHale.