The ninth-seeded Williams moved into the quarterfinals in New York by outlasting Carla Suarez-Navarro 6-3, 3-6, 6-1 on Sunday, while Anastasija Sevastova delivered a 5-7, 6-4, 6-2 win over Sharapova.
American Sloane Stephens also reached the quarterfinals by prevailing 6-3, 3-6, 6-1 over 30th-seeded German Julia Goerges.
No. 13 seed Czech Petra Kvitova posted a 7-6 (3), 6-3 victory over third-seeded Spaniard Garbine Muguruza in a battle of two-time Grand Slam champions. Kvitova's victory under the lights at Arthur Ashe Stadium set up a match in the quarterfinals versus Williams, against whom she is 4-1 in her career.
While a Williams sister -- Serena gave birth to a baby girl on Friday -- typically makes it this far at the U.S. Open, it was a surprise that wild-card entrant Sharapova lasted until Sunday.
Sharapova served a 15-month suspension for testing positive for the banned substance meldonium at the 2016 Australian Open and this was her first major tournament since recently returning to the tour.
The Russian won three matches -- including an upset of No. 2 seed Simona Halep -- before succumbing to Sevastova. Sharapova hit 42 winners but also committed 51 unforced errors while the Latvian had just 14 during the two-hour, 16-minute match.
"It's been a really great ride in the last week," Sharapova said afterward. "Obviously coming off a loss, you know, it's a quick turnaround in order to reflect all the positives that happened in the last eight or nine days.
"But ultimately I can take a lot from this week. It's great to get that major out of the way. It was an incredible opportunity. I'm very thankful for the opportunity. I did my best. I can be proud of that."
The 16th-seeded Sevastova is back in the U.S. Open quarterfinals for the second straight year. She fell to Caroline Wozniacki last year.
"The first set was very close and could have gone either way," Sevastova, who hit 21 winners, said of the match with Sharapova. "I think she played unbelievable the first and second sets and I just kept fighting. I was running every ball just trying to save it."
Sevastova faces Stephens in the quarterfinals. It has been a strong summer for Stephens after being off the tour for 11 months due to a foot injury.
"When I started playing at Wimbledon -- that was my first tournament back -- my foot, I wasn't really sure," said Stephens, who is in the U.S. Open quarterfinals for the first time. "I wasn't trusting in it because I was kind of afraid because I didn't want to fall. I didn't want to hurt myself again."
Williams is 19-3 in majors this year and looking to win her third U.S. Open title. Her victories were in 2000 and 2001 so the 37-year-old is eager to add another to the tally.
"For me, I am focused on myself and trying to be aggressive as possible," Williams said. "No one ever gives you a slam. You got to take it. So I know that so I am going to try to take it."
Williams has won seven Grand Slam titles in her career, the most recent being Wimbledon in 2008.
Williams reached this year's Wimbledon final before falling to Muguruza.
Muguruza won't be contending in the final of this major, however, despite jumping out to a 4-1 lead in the opening set.
Kvitova held strong, however, winning five of six games on the way to capturing the set in a tiebreaker.
Muguruza broke her foe to begin the second set before Kvitova turned it on again to advance to her second quarterfinal appearance of the U.S. Open. The 27-year-old also gained a berth in the quarters in 2015, when she lost to eventual champion Flavia Pennetta.