In a letter to the PM he said the government "owe it to people who have sacrificed so much in this pandemic to be honest when we have let them down".
Boris Johnson said he was "sorry" to receive the resignation.
Former chancellor Sajid Javid has said he is "honoured" to have been chosen as Mr Hancock's replacement.
Mr Hancock had been under increasing pressure to quit, after the Sun published pictures and then a video of Mr Hancock and Gina Coladangelo, who are both married with three children, kissing. The newspaper said they had been taken inside the Department of Health on 6 May.
Mr Hancock has ended his 15-year marriage to his wife, Martha, and the relationship with Ms Coladangelo is understood to be a serious one.
Fellow Tory MPs, as well as Labour and the Covid-19 Bereaved Families for Justice group, had called for the health secretary to be sacked and a senior Tory figure told the BBC that many MPs had told their whips on Saturday that he ought to resign.
BBC political editor Laura Kuenssberg said No10 had stressed that it had been Mr Hancock's decision to go and that he had not been pushed out by the prime minister.
She said Ms Coladangelo was also leaving her role as a non-executive director of the Department of Health.