He moved back to Delhi and joined both the Indian National Congress and the Muslim League. He played an important role in the negotiation of the 1916 Lucknow Pact and served as the League's president in 1918 and 1920. He was an outspoken supporter of the Khilafat movement, and worked to bring the official Khilafat body, the League and the Congress Party together on the issue against the Mustafa Kemal's decision to oust the Sultan of Turkey, who was the Caliph of Islam, and to protest the recognition of Turkey's independence by the British Empire. Dr. Ansari served serveral terms as the AICC General Secretary, as well as the President of the Indian National Congress during its 1927 session. As a result of in-fighting and political divisions within the League in the 1920s, and later the rise of Muhammad Ali Jinnah and Muslim separatism, Dr. Ansari drew closer to Mahatma Gandhi and the Congress Party. Dr. Ansari also served as the chancellor of the Jamia Millia Islamia university in Delhi soon after the death of its founder, Dr. Hakim Ajmal Khan. Dr. Ansari's wife was a deeply religious woman, who worked with him for the upliftment of Delhi's Muslim women. The Ansaris lived in a palatial house, called the Darus Salaam or Abode of Peace in Urdu. The Ansaris would often host Mahatma Gandhi when he visited Delhi, and the house was a regular base for Congress political activities. However, he never stopped practicing medicine, and often came to the aid of Indian political leaders and the Indian princely order. Dr. Ansari was amongst a new generation of Indian Muslim nationalists, which included Maulana Azad, Muhammad Ali Jinnah and others. He was very passionate about the issues of common Indian Muslims, but unlike Jinnah, was resolutely against separate electorates and opposed Jinnah's viewpoint that the Muslim League could be the only representative of India's Muslim communities. Dr. Ansari was very close to Mahatma Gandhi and an adherent of Gandhism, with his core teachings of ahimsa and non-violent civil resistance. He enjoyed an intimate friendship with the Mahatma. Dr. Ansari died in 1936 en route from Mussoorie to Delhi on a train due to a heart attack, and is buried in the premises of the Jamiya Milliya Islamiya in Delhi.