It happened because back when Pennsylvania was the capital of the U.S. Veterans of the Continental Army was protesting that Congress hadn't fulfilled their promises to provide them with pay, and so they marched on Congress in Philadelphia. Congress asked the Philadelphia Executive Council to use the city militia to protect Congress. The Council refused, and so Congress had to flee the city to Princeton, NJ, for their own protection from the protesting armed soldiers.
That caused Congress to see the need to make a capital in which the federal branches could see to their own security needs rather than a city or state government. This is why Washington, D.C. is a federal district - they don't have to worry about a city or state government undermining them.
But, then again, our understanding of federalism has changed since then. Now, for the most part, federal power supersedes state and city power. Also, the federal government has federal police agencies (FBI, Secret Service, Homeland Security, etc.) that can work with or independently of local agencies, which renders the original need for it to be specifically a federal district moot.
So nowadays I think that Washington, D.C. would be better served with statehood. The federal buildings can still be administrated by the federal government but the local government would have more control over local issues, which would thus depoliticize their local issues. It would also give the people of Washington, D.C., representation in the federal government.