Those two were meeting Saturday night in a battle of former world number ones.
Williams, playing a third tournament in a row for the first time since late 2007, improved her 2014 match record to 37-6.
The 32-year-old said that she needed a set to find her game after a slow start in a match which featured 15 breaks of serve, seven of them in the final set.
"I got off to a little bit of a slow start, but I was definitely in it," said Williams, who followed up her win in Stanford by reaching the semi-finals last week at Montreal, where she lost to her sister Venus. "I had to get out there and grind out everything.
"She's a really good returner and she was getting everything back. She just made some good shots.
"I gave all that I had in me today," she said. "I didn't expect to do this well over the summer. I knew I needed matches so I entered three tournaments. But I'm still here."
Wozniacki came to the match after beating two top 10 players in a row -- Angelique Kerber and Agnieszka Radwanska -- for first time since the 2010 WTA Finals.
Despite the defeat, Wozniacki is ready for one last US Open tune-up next week in New Haven.
"I've had a lot of matches these couple of weeks," said the 24-year-old Dane. "But I'm good with playing New Haven. I'm going to use it as even better preparation, get hopefully a few matches in.
"There are a few things that I just want to keep working on."
As for her performance against Williams, Wozniacki would have liked to have served better.
"I just didn't serve well today," she said. "Didn't get many first serves in. I was fighting, I was running. I was trying to take the ball early. I was returning well.
"Then my serve comes around and I can't seem to hold serve. It's a frustrating thing when you're out there on the court."
Wozniacki, who lost to Williams in three sets in the Montreal quarter-finals, won the opening set in half an hour despite double-faulting away a service game.
But Williams stepped up a gear in the second, helped to an early break by another of Wozniacki's double faults.
- Third-set roller-coaster -
The third was a roller-coaster for both players, who are good friends off the court.
After five straight breaks of serve, Williams finally stopped the rot as she held for a 4-2 lead.
She secured another break for 5-2 but then had to face a Wozniacki fightback, with the former number one reaching 4-5.
Williams finally closed it out in the next game after an hour and three-quarters. She finished with seven aces and benefitted from eight Wozniacki double faults.
"I knew I had to return better, and today I was able to do that," Williams said. "I actually felt better than in my other matches."
Russian superstar Sharapova, who leads Ivanovic 8-3 in their career series, was not expecting an easy time on court against the Serb.
"The tournament is still not done," the French Open champion said. "I certainly hope to raise my level and have a much better first set.
"It can't be any worse," she added in reference to her slow start against Simona Halep in the quarter-finals. "I'm just happy to be in that stage."