How did the Barog get inside a mountain in Moria/Khazad Dum?
He probably escaped Beleriand during the War of Wrath via underground tunnels (remember that Angband had very deep pits) and hid deep in the earth. He was probably dormant until the Dwarves delved to deep in the earth and awoke him.
The Balrog must have found a cave entrance that led deep underground. It might have even been the same entrance discovered later by Durin the First.
EDIT: Well, probably not the same entrance as Khazad-dum was well-established by the end of the First Age.