Jannik Sinner dug himself out of a hole to defeat Denis Shapovalov and reach the fourth round of the US Open.