The seven-time grand slam winner saved four match points before Olympic gold medallist Sun converted her fifth chance with a forehand pass to win the tiebreak 9-7.
Fourth seed Williams looked close to tears as she served in the tiebreak but she refused to make excuses for her worst loss since joining the WTA Tour full-time in 1997.
Sun, who won Olympic doubles gold with Li Ting in Athens last year, became the second Chinese player to reach the last eight in Beijing.
China’s number one Peng Shuai beat Argentine Mariana Diaz-Oliva 3-6 6-2 7-6 to reach her fourth WTA quarter-final this year.
The world number 33 battled nerves to storm through the tiebreak 7-0 and will now play Germany’s number nine seed Anna-Lena Groenefeld, who ousted Japan’s Ai Sugiyama 6-2 6-3.
Peng’s compatriot Zheng Jie produced an upset with a 7-5 4-6 7-5 win over sixth seed Jelena Jankovic in a first round match on a successful day for the home players.
Second seed Lindsay Davenport withdrew with injury to simplify Wimbledon champion Venus’s route to the final.
New world number one Sharapova faces Shahar Peer in her opening match on Thursday after the Israeli’s 6-2 6-0 win over another Russian, Vera Douchevina.
Frenchwoman Tatiana Golovin was another seed to fall on Wednesday, the number seven losing 6-3 6-4 to Japan’s Shinobu Asagoe in a first round match.
RESULTS
Second round: Tiantian Sun (China) bt Serena Williams (United States) 6-2 7-6(7);
Anna-Lena Groenefeld (Germany) bt Ai Sugiyama (Japan) 6-2 6-3;
Shuai Peng (China) bt Mariana Diaz-Oliva (Argentina) 3-6 6-2 7-6(0)
First round: Jie Zheng (China) bt Jelena Jankovic (Serbia and Montenegro) 7-5 4-6 7-5;
Marta Domachowska (Poland) bt Meghann Shaughnessy (United States) 6-3 6-3;
Shinobu Asagoe (Japan) bt Tatiana Golovin (France) 6-3 6-4;
Shahar Peer (Israel) beat Vera Douchevina (Russia) 6-2 6-0.
KOLKATA RESULTS
Kolkata: Results from the Kolkata Open Women’s Singles matches on Wednesday
Second round: Melinda Czink (Hungary) bt Sania Mirza (India) 0-6 6-4 6-4;
Kaia Kanepi (Estonia) bt Virginia Ruano Pascual (Spain) 6-7 7-6 6-1;
Shikha Uberoi (United States) bt Rika Fujiwara (Japan) 6-2 6-3
First round: Galina Voskoboeva (Russia) bt Victoria Azarenka (Belarus) 6-3 7-5;
Karolina Sprem (Croatia) bt Evie Dominikovic (Australia) 6-4 7-5;
Sybille Bammer (Austria) bt Nicole Pratt (Australia) 6-1 6-3;
Elena Likhovtseva (Russia) beat Yuliana Fedak (Ukraine) 6-1 6-3 —Reuters