The story concerns three students from the Brothers' Monastery at Kiev. Every summer, after classes have ended, there is usually a large procession of all the students moving around the area as they travel home, getting progressively smaller as each student arrives at his home. Eventually, the group is reduced to three students, the theologian Khaliava, the philosopher Khoma Brut, and the rhetorician Tibery Gorobets.
As the night draws in, the students hope to find a village near the main road where they can find some rest and food. However, they become lost in the wilderness, eventually coming upon two small houses and a farm. An old woman there tells them she has a little room and cannot accommodate any more travelers, but she eventually agrees to let them stay. The rhetorician is put in the hut, the theologian in an empty closet, and the philosopher in an empty sheep’s pen.
At night, the old woman comes to Khoma. At first, he thinks she is trying to seduce him, but then she draws closer and he sees that her eyes are glowing strangely. She leaps on his back, and he reluctantly finds himself galloping with her all over the countryside with a strength he previously never knew. He eventually slows her by chanting exorcisms out loud, and then rides on her back and later picks up a piece of wood and beats her as punishment. The old woman later collapses, and he discovers she has turned into a beautiful girl.
Khoma runs away to Kiev and resumes his easy life, when a rumor reaches his dean that a rich cossack’s daughter was found crawling home near death, her last wish being for Khoma the philosopher to come and read psalms over her corpse for three nights after her death.
Although Khoma is uncertain why the girl requested him specifically, the bribed dean orders him to go to the cossack’s house and comply with her last wish. Several Cossacks bring him by force to the village where the girl lived. When he is shown the corpse, however, he finds it is the witch he overcame earlier in the story. Rumors among the Cossacks are that the daughter was in league with the devil, and they tell horror stories about her evil ways, such as previously riding on another person, drinking blood, and cutting off the braids of village girls. Therefore, Khoma is reluctant to say prayers over her body at night.
On the first night, when the cossacks take her body to a ruined church, he is somewhat frightened but calms himself when he lights more candles in the church to eliminate most of the darkness. As he begins to say prayers, he imagines to himself that the corpse is getting up, but it never does. Suddenly, however, he looks up and finds that the witch is sitting up in her coffin. She begins to walk around, reaching out for someone, and begins to approach Khoma, but he draws a circle of protection around himself that she cannot cross. She gnashes her teeth at him as he begins to exorcise her, and then she goes into her coffin and flies about the church in it, trying to frighten him out of the circle. Dawn arrives, and Khoma has survived the first night.
The next night similar events occur but more horrible than before, and the witch calls upon unseen, winged demons and monsters to fly about outside the church, but Khoma is invisible for them. When the cossacks find the philosopher in the morning, he is near death, pale and leaning against a wall. He tries to escape the next day but is captured and brought back to finish.
On the third night, the witch’s corpse is even more terrifying, and she calls the demons and monsters around her to bring Viy into the church, who can see everything. Khoma realizes that he cannot look at the creature when they draw his long eyelids up from the floor so he can see, but he does anyway and sees a horrible, iron face staring at him. Viy points in his direction, and the monsters leap upon him. Khoma dies from horror. However, the monsters miss the first crowing of the rooster and are unable to escape the church when day begins.
The priest arrives the next day to find the monsters frozen in the windows as they tried to flee the church. The temple is forsaken forever, eventually overgrown by weeds and trees. The story ends with Khoma’s other two friends commenting on his death and how it was his lot in life to die in such a way, agreeing that he only came to his end because he flinched and showed fear of the demons.