In the literary works by J. R. R. Tolkien collectively called Middle Earth, Moria is the name given to the underground city, mines, and connected tunnels that run through the central Misty Mountains. There, for three ages of Middle Earth, a thriving dwarf community created the greatest dwarven city ever known. However, by the end of the Third Age, at the time of the events of The Lord of the Rings, Moria had become a dark and cursed place, and dwarves no longer lived there, but only orcs, trolls, and other evil entities.
Before the First Age, the father of the dwarves, named Durin, woke by a lake in the Misty Mountains. This lake, called Mirrormere or Kheled-zaram, was later a revered place among dwarves. Nearby, Durin first began his city, which was called Khazad-dûm by the dwarves and also Dwarrowdelf by men and Hadhodrond by elves. Afterwards, other rulers of Khazad-dûm were sometimes named Durin, who the dwarves believed came to live again among his people.
In the Second Age, the Dwarves of Khazad-dûm forged a friendship with the Noldorian elf realm of Eregion - but this friendship ended in disaster with the forging of the Rings of Power, the rise of Sauron wielding the One Ring, and the destruction of the elven realm. Then Khazad-dûm was closed, and Sauron could not enter it.
In the Third Age, the dwarves, seeking a precious metal called mithril, delved too deep and awakened a Balrog, a evil fire spirit from the elder days. This spirit, called Durin's Bane, fought the dwarves, and they were forced to flee their ancient home. After that, the realm was known as Moria, the Black Pit. Several dwarven generations later, Balin, who had accompanied Bilbo Baggins on the Quest of Erebor described in The Hobbit, led a new group of dwarves to reopen the city. At first all went well, but after a few years the community was destroyed by orcs and similiar creatures, although their fate was initially unknown.
In The Lord of the Rings, when Frodo Baggins set out from Rivendell with the Fellowship, they at first planned to travel over the Misty Mountains. When they were stopped by snow on Mt. Caradhras, they found themselves pursued by wolves and orcs, and fled into Moria, so as to go under the mountains. There they found Balin's journal and learned the fate of his expedition. They were then set upon by a host of trolls, orcs, and the Balrog. Gandalf fought the Balrog on a narrow bridge and succeeded in destroying a section of bridge to make the Balrog fall. As it fell, the Balrog snagged Gandalf's leg with its whip and pulled him after it, sending them both plunging into a deep abyss where they continued to fight, Gandalf eventually overcoming the creature. The rest of the Fellowship, believing Gandalf dead, managed to escape Moria and reach Lothlorien mostly unharmed.