Roger Federer began the defence of his Shanghai Masters title with a hard-fought second-round victory over Daniil Medvedev on Wednesday.

In his first outing since a surprise US Open fourth-round defeat to John Millman last month, Federer ousted the in-form Russian 6-4 4-6 6-4 in a high-quality encounter.

Medvedev came into this clash on the back of his third career title in Tokyo on Sunday, while only Juan Martin del Potro has more hard-court wins in 2018 than the world number 22.

And what looked like a stern test for Federer on paper proved to be exactly that in reality as the Swiss was taken the distance before his opponent ultimately crumbled at the crucial juncture late in the third set.

Federer, chasing a third crown in Shanghai, will face Roberto Bautista Agut in the next round as he continues his preparations towards the ATP World Tour Finals.

Throughout much of the contest, Medvedev looked assured in exalted company after recovering from a nervy opening which saw him start with a double-fault as Federer broke in the first game.

Medvedev was soon back on serve at 2-2 but Federer turned the screw at 4-4, breaking again as his opponent went long before a serve and volley saw him edge ahead.

In the second, the Swiss was forced to applaud a stunning backhand volley on the stretch from Medvedev at 2-2 – one that actually saw the ball land on the Russian's side of the net on the second bounce – and the world number two was taken to a decider as the underdog put away a crunching forehand.

A thrilling deciding set unfolded with each man saving a break point until, at 4-4 30-30, Medvedev produced two awful forehands to present his opponent with an opening. It was an invitation unlikely to be passed up, Federer seeing out victory with a resounding ace out wide.