In the Second Age, Isildur brought with him a three meter tall stone as a symbol of his lineage and Gondor. He set it on a hill in Erech. He made the local hill tribes swear an oath of loyalty on the stone. The hill tribes broke their oath and were doomed to haunt the place as wraiths.
The city was completely abandoned at the end of the Third Age, when the heir of Isildur, Aragorn, came to Erech to hold them to their oath again. This time they held true to their oath and formed the host of the Dead and helped Aragorn fight the Corsairs of Umbar at Pelargir.
After the battle at Pelargir Aragorn released them from their oath and they were allowed to rest.