 Ansari contested the general election of the Lok Sabha in 2004 on the Samajwadi Party ticket and defeated Manoj Sinha from Bharatiya Janata Party with a margin of 2,26,777 votes.He contested the 2009 general election on the Bahujan Samaj Party ticket from Ghazipur, Uttar Pradesh, but lost to the Samajwadi Party's Radhe Mohan Singh.After some political differences, he left the Bahujan Samaj Party and founded a new political party, Quami Ekta Dal.