Chang Kai-chen, an 18-year-old from Taiwan, upset the top-ranked Safina 7-6 (5), 4-6, 7-5. Russian teenager Anastasia Pavlyuchenkova edged Williams 7-6 (6), 7-5 to reach the third round.
Safina was serving for the match in the third set when she double-faulted to give up a break. Chang, ranked 132nd and playing only the fourth time in a WTA main draw, held her serve and broke Safina again for victory.
"The double-fault didn't affect me that much," Safina said. "Whether it's a double-fault or a mistake it's the same. It's just the way I played at 5-4. It wasn't the right game, I didn't use my first serve the whole game."
A dejected Williams made a hasty exit from Ariake Colosseum, saying only she was looking forward to her next match in Beijing.
Williams took a 3-0 lead in the first set but the 18-year-old Pavlyuchenkova fought back, breaking Williams to tie it 5-5 before holding serve and winning the tiebreak.
Pavlyuchenkova hit a forehand down the line to go up 6-5 in the second set. She had a triple-match point in the 12th game and won when Williams' return was long.
Chang converted her third match point on Safina's forehand error after the Russian had fought back from 0-40 to 30-40.
"On match point, I was just thinking the same thing as always," Chang said. "I wasn't thinking 'Oh, I have match point, I'm going to win.' "
Chang said her thoughts turned to her parents after the upset.
"My mother doesn't get out to watch a lot of matches but she follows the live scoring, and I was thinking about what she thought when she saw today's score," Chang said.
Chang made her Grand Slam debut at this year's U.S. Open, advancing to the second round.
In other matches, Andrea Petkovic upset fifth-seeded Svetlana Kuznetsova 7-5, 4-6, 6-3. Kuznetsova lost to Safina in last year's final here. Veteran Ai Sugiyama, playing in her last singles match, was forced to retire while trailing 6-0, 2-1 to Nadia Petrova.
The 34-year-old Sugiyama, who announced this month she will retire after 17 years on tour, said she was feeling ill.