The top-seeded American looked set to close the match when she broke serve to go 5-2 up, but squandered the lead and then trailed 4-0 in the decisive tiebreaker. She saved a match point before going on to win.
"I definitely feel really proud because I definitely wasn't playing my best tennis and I was far off playing well," she said. "Also, at that point I wasn't going to lose. After three hours, I'd better win."
Williams left the court for treatment when ahead 3-2 in the third set. She returned with a strapped right thigh and does not yet know the extent of the injury.
Williams wasted three set points in the first and then faced a match point on her serve when trailing 6-5 in the second, but held on despite a slew of unforced errors.
In another second-round match, Francesca Schiavone beat Sybille Bammer 6-2, 6-1.
In the first round, Peng Shuai advanced when 10th-seeded Victoria Azarenka retired while trailing 3-0. Other seeded winners were No. 8 Samantha Stosur, No. 14 Flavia Pennetta and No. 16 Nadia Petrova.
In the men's first round, 12th-seeded Gael Monfils of France beat compatriot Stephane Robert 6-2, 7-5. Qualifier Oleksandr Dolgopolov Jr. of Ukraine downed Andrea Seppi of Italy 6-3, 6-4 and will meet Rafael Nadal in the second round. Austria's Jurgen Melzer topped South African qualifier Kevin Anderson 6-2, 2-6, 6-4 to set up a meeting with David Nalbandian or Tomas Berdych.