His song sold 3,000 copies more than Sheeran's, with just under 40,000 downloads and 3.5 million streams, the Official Charts Company said.
Sheeran's Shape of You fell to number two, while his Galway Girl is at three.
The Official Charts Company added that Shape Of You is still the most streamed song of the week, with nearly seven million plays.
Styles, whose debut album is released next month, is the second member of One Direction to top the singles chart, after Zayn Malik managed the same feat in 2016 with Pillowtalk.
Sheeran has also retained his domination of the album chart, with Divide still on the top spot, followed by Rag 'N' Bone Man's Human and then Chainsmokers with Memories - Do Not Open.
In its first week, Divide outsold the rest of the top 500 combined and became the fastest-selling album ever by a male artist.
Last month Sheeran also held 16 places out of the top 20 in the singles chart, including numbers one, two, three, four, five and six - not surprisingly, this was a chart first.
More about: #HarryStyles