The 30-year-old, who had been due to face Federer, confirmed that he had decided to pull out of the tournament in New York after seeking medical advice.
"I am really sorry for Mardy. I just want to wish him a speedy recovery," Federer said in a statement.
"We all want to see him back on Tour soon."
Federer will now advance through to the quarter-finals at Flushing Meadows, where he will take on Tomas Berdych.