Brahma kept condition that whoever surrounds three worlds first shall marry Ahalya. Indra wanted to marry Ahalya. He used all his magical power to go around the three worlds and reached Brahma to take the hand of Ahalya. But sage Narada came and announced that Rishi Gautama had gone around the three world even before King Indra.He mentioned that as a part of a deity puja, Gautama went around a cow at his hermitage while the cow gave birth to a calf. According to Vedas, the cow at the time of bearing of the child is equal to the three worlds and Ahalya must be given to Gautama, not Indra. Thus Ahalya was married to Rishi Gautama.
Ahalya's beauty attracted the eyes of Indra, who was the king of the gods. He was looking for an opportunity whenever he could go near Ahalya and allure her. But the Rishi was always near Ahalya so he got no opportunity. At last he decided to take a devious means and approached the Sun God. The Sun god denied helping him in a diplomatic manner. But the Moon God gave him information that sage Gautama goes to river to bathe early each morning. He also said that the Rishi is away for a considerable period of time so that Indra could ravish with Ahalya without any fear of being caught.
Moon also helped Indra by other means. He imitated the rooster's call in the middle of the night. Sage Gautama thought it was dawn and went to the Ganga. Indra knocked on Ahalya's door in the guise of sage Gautama. Ahalya immediately saw through the disguise but he could not refuse Indra due to her vanity. She thought she was so beautiful that even king of the gods is charmed by her beauty. She asked coyly why the Rishi was back so soon. Indra said that he felt to be with her.
Meanwhile the Rishi reached Ganga and as he stepped inside the water the holy Ganga informed him the unholy alliance taking part in his ashrama. The sage became furious and returned back to his hut. The moon was guarding the door in the guise of a cock. Angrily Gautama smote the cock with his towel, which left a permanent black mark on moon. Ahalya helped Indra to hurry through the door as they completed their lovemaking. Indra bumped against the angry sage. The sage gave them curse. Indra's testicle fall down and his body was covered with thousand vaginal marks.
Now the sage turned his attention to his wife. He cursed her so that she turned into rock, lying to ashes, invisible to all creatures and unable to respond to anyone. Ahalya begged forgiveness. He modified the curse saying when Lord Vishnu will touch as his incarnation 'Rama', Ahalya would come to life again, free from lust and follyand completely purified.
True to Gautama's word, Vishnu took his seventh birth as the Avatar Rama. When Rama was going to Sita's Swayamvar along with sage Vishwamitra and his brother Laxman, the beauty of the desolated hermitage attracted him. Vishwamitra narrated the entire episode. Rama then touches the rock with his foot and brought Ahalya to her life. Ahalya was now redeemed from any sin and was grateful to rama.sage Gautama also appeared and blessed Rama. Gautama and ahalya lived happily thereafter.