In a match featuring a super tie-break and lasting one hour and seven minutes, Ostapenko won 6-2, 3-6, 10-5.
The 36-year-old American, winner of 23 grand slam single titles, was making a comeback after taking nearly 11 months off to have her first child.
Her last match was the Australian Open final, which she won despite being two-month pregnant.
“It was good to be back out there. I missed playing, I missed competition, I missed the crowd, the atmosphere so much. It was really nice to be back,” said Williams after the match.
“I don’t know if I am totally ready to come back on the Tour yet. I know that when I come back I definitely want to be competing for championships,” she said, refusing to commit yet to the Australian Open, which starts in a little over two weeks.
“I don’t know, but I am definitely looking forward to getting back out there.
“I am taking it one day at a time. I am going to assess everything with Patrick (Mouratoglou, her coach) and my team,” she said.
It was the first time Ostapenko had played against Williams, whom she described as her “childhood idol.”
The 20-year-old Latvian was born almost two years after Williams turned professional in September 1995.
Williams was clearly having problems with her serve and was broken five times in the 17 games.
She also stuck mostly to the baseline and only twice charged the net.
She started well, breaking Ostapenko’s serve in the first game of the match. But Williams’ inability to get her first serve going, along with several unforced errors early in the match, allowed the Latvian to break twice and build a 4-1 lead before wrapping up the set 6-2.
Williams’ problems with her serve continued, but she unleashed a few of her trademark double-handed shots on both sides to break Ostapenko twice at the start of the second set to go 3-0 up. By this time she was moving better and she started hitting more winners.
“That’s the really good thing. In the beginning, it felt a little tough. But as the match moved on, I was less afraid,” explained Williams.
“I knew I was not going to fall over and break. The more I played, the more confident I felt that I would be able to go for shots that I was afraid to go for in the first set.
“It was a wonderful opportunity for me to kind of test where I am at. Not just physically, but also mentally.”
The super tie-break was mostly a one-sided affair as Williams made unforced errors trying to be aggressive.
She was down 2-8 at one stage and won the next three points, but Ostapenko held her serve twice to close the match.