Roger Federer opened the defence of his Shanghai Masters title with an absorbing three-set victory against an in-form Daniil Medvedev.

The world No 2, playing his first competitive match since his surprise US Open fourth-round exit, found some of his best tennis in the latter stages in a 6-4 4-6 6-4 win against the Japan Open champion.

Two-time champion Federer will next face Roberto Bautista Agut, after the unseeded Spaniard beat Mackenzie McDonald 3-6 6-4 6-1.

Federer began strongly to break the Russian in the opening game of the match but was quickly broken back by the world No 22 as both players searched for their best tennis.

With the first set well-balanced at 4-4, Federer took advantage of a drop in focus from Medvedev to claim a second break of serve before the 20-time Grand Slam champion held serve to secure the initiative.

Medvedev saved an early break point before a backspin drop-shot-volley winner in the fifth game caught Federer by surprise as the Russian grew in confidence.

He produced impressive baseline hitting to trouble Federer and was rewarded when he converted his first break point of the set to force a decider.

Both players saved a break point in a high-level opening to the third set but Federer finally secured the crucial breakthrough before an ace completed a hard-fought victory.