Ayni was born in a peasant family in the village of Soktare in what was then the Emirate of Bukhara. He became an orphan at 12 and moved to join his older brother in Bukhara, where he attended a madrasa and learned to write in Arabic.
In the early 1920s Ayni helped to propagate the Russian Revolution in Uzbekistan and Tajikistan. In 1934 he attended the Soviet Congress of Writers as the Tajik representative. By purporting national identity in his writings, he was able to escape the Soviet censors that quieted many intellectuals in Central Asia. Ayni survived the Soviet Purges, and even outlived Stalin by one year. He was member of the Supreme Soviet of Tajikistan for 20 years, was awarded the Order of Lenin three times, and was the first president of the Academy of Sciences of Tajik SSR. After 1992, his writing helped to bind together a sense of Tajik nationalism that survived the collapse of the Soviet Union.
Ayni gave indigenous Tajik literature in Tajikistan a boost in 1927 by writing Dokhunda, the first Tajikistani novel in the Tajik language. In 1934 and 1935, leading Russian director Lev Kuleshov worked for two years in Tajikistan at a movie based on Dokhunda but the project was regarded with suspicion by the authorities as possibly exciting Tajik nationalism, and stopped. No footage survives. Ayni’s four-volume Yoddoshtho (Memoirs), completed 1949-54 are famous and widely read. In 1956, Tajik director Boris (Besion) Kimyagarov (1920–1979) was finally able to get approval for a movie version of Dokhunda.
Ayni’s early poems were about love and nature, but after the national awakening in Tajikistan, his subject matter shifted to the dawn of the new age and the working class. His writings often criticized the Amir of Bukhara. Two well-known are The Slave and The Bukhara Executioners.
Ayni died in Dushanbe, the capital of Tajikistan, where a mausoleum stands in his honor.