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 year 1980 of the Third Age, however, the Dwarves, seeking a precious metal called mithril, delved too deep and awakened Durin's Bane, an evil fire spirit from the elder days. This spirit fought the Dwarves and killed Durin VI, the King of Khazad-dûm. The following year, it also killed Náin, Durin VI's son, and the Dwarves were forced to flee their ancient home. After that, the realm was known as Moria, the Black Pit.
Eventually, Sauron began to people the Pit with his followers, mainly orcs and trolls, and they discovered that the terror was, in fact, a Balrog of Morgoth. In 2799 of the Third Age, a battle was fought on Moria's East Gate, and the Dwarves were successful in driving away Sauron's minions, but they could not defeat the Balrog. 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 similar 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 of thongs and pulled him after it, sending them both plunging into the abyss spanned by the bridge. The rest of the Fellowship managed to escape Moria and reach Lothlórien mostly unharmed.
Unknown to the Fellowship, both Gandalf and the Balrog survived the fall and fought a ferocious battle from the depths of Moria to the mountains above, demolishing large parts of Moria in the process. Gandalf cast down the Balrog upon the mountainside and lived just long enough to see it die, but his story was not yet ended. (See his entry for further details.)
While Gandalf had felled the Balrog and most of the orcs of Moria died in the war of the ring it was too late for the great city. A thousand years of abandonment and the battle at the endless stair and Durin's tower had wreaked much of the city and it is believed that the dwarfs have never again lived in Moria. At most, they may have retrieved what they could of their once-mighty riches but most seem to have remained at the three great citys of the fourth age, The Iron Hills, The glittering caves under Gimli and the refounded Kingdom under the mountain at Erebor.