Caroline Wozniacki came through a three-set battle against Ashleigh Barty to reach the third round of the Madrid Open.

The second seed, who will move to the top of the rankings if she claims the title in the Spanish capital, rallied from a break down in the decider to triumph 6-2 4-6 6-4 after two hours and six minutes on court.

Having dropped just four games against Daria Gavrilova on Sunday, Wozniacki looked on course for another routine win when she breezed through the first set. 

However, Barty - who lost the opening four games of the match - recovered from a shoddy start to make it a contest.

She produced 18 unforced errors to fall behind but improved dramatically in a competitive second set that stayed with serve until the seventh game.

Wozniacki broke to love to go 4-3 ahead, only to immediately surrender the advantage to Barty, who won three games on the spin to draw level despite struggling with her first serve.

The upset appeared to be on when the Australian edged ahead in a tense third set, a well-earned break of serve putting her 4-2 up. 

Yet Wozniacki capitalised on a moment of good fortune, a drop shot clipping the net before dropping just over the other side of the court, to hit back immediately, sparking a four-game run that wrapped up a hard-fought victory.

Next up will be Kiki Bertens, who knocked out 15th seed Anastasija Sevastova 6-1 6-4 on Monday, as the Dane aims to replace Simona Halep as world number one.