Ansari had announced his candidature from Varanasi last month. He pulled out to prevent a division of Muslim votes against Modi, his supporters said.
Lodged in central jail in Agra since 2010 for his alleged role in the murder of a BJP legislator, Krishnand Rai, Ansari lost to BJP leader Murli Manohar Joshi in Varanasi in the 2009 Lok Sabha polls.
But his brother, Afzal Ansari, surprised everyone this week saying there was a "Modi wave" in the country and that the Gujarat chief minister would win in Varanasi.
Close aides said Ansari had completed the formalities of contesting the Lok Sabha polls from inside the prison after the returning officer of Mau okayed his candidature for Ghosi.