Maria Sharapova avoided an early exit from the Madrid Open on Tuesday, rallying to beat Christina McHale to reach the third round while Roger Federer withdrew from the men's event.

Trailing 4-1 in the final set, Sharapova steadied her erratic service game and took command again to beat the 56th-ranked American 6-1, 4-6, 6-4 on clay at the Magic Box tennis center.

The ninth-ranked Russian looked to be cruising before McHale broke late in the second set to tie the match and then took her commanding lead in the final set after breaking Sharapova.

However, Sharapova didn't surrender a point after that as McHale netted to be broken to see her advantage dip to 4-3, and Sharapova eventually fired an ace down the middle to eliminate McHale for the second year running.

"After a really good start (in Madrid) I didn't keep that level. I think I had a bit of a letdown," said 2013 finalist Sharapova, who is coming off a tournament victory in Stuttgart.

Earlier, Li Na continued her recent dominance of Zheng Jie by easing into the third round 6-2, 6-3 for her fourth straight victory against her fellow Chinese player. Zheng hasn't won a set from the second-ranked Li since her last victory in 2006.

Lucie Safarova of the Czech Republic also advanced with a 6-1, 6-2 win over Magdalena Rybarikova of Slovakia, while American Sloane Stephens was also a winner.

Austrian qualifier Dominic Thiem scored the biggest victory of his young career when he rallied for a 1-6, 6-2, 6-4 victory over third-ranked Stanislas Wawrinka.

The 20-year-old Thiem showed composure beyond his years in knocking the Australian Open champion out of the clay-court event to reach the third round.

Thiem called on a thunderous array of baseline strokes and soft net touches to oust the third-seeded Wawrinka, who reached the final at Madrid's Magic Box tennis center last year.

Thiem looked out of his depth as he struggled to find his rhythm early on Manolo Santana center court.

"I didn't have many matches against these top guys, so I wasn't really used to his pace. He had an unbelievable start and I didn't really know what happened," the 70th-ranked Thiem said. "I started to get used to more and more his pace and angles and his game. I played unbelievable the second and the third set."

Wawrinka may have dropped his guard after the easy opening as Thiem pulled even after bombarding his opponent with powerful backhanders.

Wawrinka, coming off a clay court win in Monte Carlo, gathered himself for the final set -- with both players holding serve until the 10th game when Thiem slapped another strong backhand across Wawrinka and down the line for 30-15. A gentle drop shot from Thiem following a long rally of groundstrokes set up match point on Wawrinka's serve.

"I was in this famous `zone' during the match. I was really unbelievable concentrated," said Thiem, who scored his first victory over a top-10 player on his third attempt.

Thiem converted his third break point to win the match as Wawrinka sent his crosscourt backhand wide.

"I had some chance in the third set (and) I should have played better when I had some opportunity," said Wawrinka, who slipped to his fourth defeat of 2014. "But I was hesitating with my game and he went for it and deserved it."

Wawrinka wasn't the only Swiss player to exit the tournament on Tuesday. Federer pulled out because of the birth of his second set of twins, this time boys -- named Leo and Lenny.

The exit of Wawrinka plus the withdrawals of Federer and second-ranked Novak Djokovic -- to injury -- give defending champion Rafael Nadal the perfect opportunity to rebound from his worst clay-court run in a decade. The top-ranked Spaniard opens on Wednesday against Juan Monaco of Argentina.

The Spanish capital's elevated altitude proved to be a problem for fifth-ranked David Ferrer, who needed almost three hours to dispatch Spanish countryman Albert Ramos 7-6 (6), 5-7, 6-3.

Ferrer appeared frustrated for much of his match, mumbling to himself and his entourage nearby before eventually coming through to win.

"You say things when you're nervous in the match that you don't have a feel for. You can write a book about a match," said Ferrer, before pointing to the effect of Madrid's thinner air on the ball. "The first day is not easy. You miss more, but that's all."