The 36-year-old American, playing in her first grand slam since giving birth to her daughter in September, said she has a muscle problem.
The showdown with Sharapova was eagerly awaited given the colourful history between the pair, and Williams' dominant record over the Russian of 19 wins to two.
However, it is Sharapova who will now progress to the quarter-finals.
Williams said: "Unfortunately I been having some issues with my pec muscle. Right now I can't actually serve so it's actually hard to play when I can't physically serve.
"I'm beyond disappointed. I gave up so much from time with my daughter and time with my family all for this moment. So it's really difficult to be in this situation."
Williams revealed she first felt the injury against third round opponent Julia Goerges on Saturday, yet she still played doubles with sister Venus on Sunday.
"That's when I started to feel it. It was really painful and I didn't know what it was," she added.
"In my doubles yesterday I tried a lot of different tapings, and I tried lots of different types of support to see how it would feel under match circumstance.
"It didn't really get a lot better.
"So I'm going to get an MRI (scan) tomorrow. I'm going to stay here and see some of the doctors here, see as many specialists as I can. And I won't know that until I get those results."
Williams admitted it was painful to walk away from what has been billed as a grudge match after she hit back at comments made by Sharapova about her in last year's autobiography.
"It's very difficult, because I love playing Maria," she said.
"You know, it's just a match I always get up for. It's just her game matches so well against mine.
"It is difficult, because like I keep saying, I have given up so much to be here.
"There are times when I'm on the court and I'm practising, and I look on the monitor and I see my daughter and she's playing and I want to be there, but I know that these are the sacrifices you have to make to live out your dream.
"I have made every sacrifice that I could. So it's extremely disappointing. But also, I made a promise to myself and to my coach and to my team that if I'm not at least 60 per cent or 50 per cent, then I probably shouldn't play."
Related tennis links