Ansari, who had been with Surrey since the age of eight, said he wanted to pursue other opportunities and was contemplating a career in law.
"After seven years as a professional cricketer and almost two decades in total playing the game, I have decided to bring my cricket career to an end," he said in a statement released by Surrey.
"This has been a very difficult decision to make and I have not made it lightly. It is... With great sadness that I say goodbye.
"While the timing may come as a surprise, I have always maintained that cricket was just one part of my life and that I have other ambitions that I want to fulfil.
More From This Section
"With that in mind, I am now exploring another career, potentially in law, and to achieve this I have to begin the process now."
Ansari was first capped by Surrey in 2014 and made his one-day debut for England the following year.
His Test debut was delayed by a freak hand injury sustained while fielding at Old Trafford in late 2015.
Ansari has combined his cricket career with academic studies.
He secured a degree in Politics, Philosophy and Sociology from Cambridge University in 2013 and was awarded a Master's degree in History by Royal Holloway, University of London last year.
Alec Stewart, director of cricket at Surrey, said: "Zafar's exceptionally tough but considered decision is one that we should all respect and understand.
"We wish Zafar the very best in whatever the future holds for him and he will always be welcomed back to the Kia Oval with open arms.