Around 350 million years ago, during the Carboniferous period, volcanic activity pushed molten basalt up through older sedimentary rock, plugging a vent with hard, erosion-resistant rock. That plug is Castle Rock, the crag on which Edinburgh Castle now stands, and a second vent further east cooled into Arthur's Seat.
Edinburgh's Old Town sits almost entirely on the protected "tail" of the landform, which is why the Royal Mile slopes gently downhill for over a mile from the Castle esplanade to the Palace of Holyroodhouse — you're walking down a 15,000-year-old glacial shadow.
A 3D geological cross-section of Edinburgh showing how a 350-million-year-old volcanic plug and a west-to-east Ice Age glacier together carved the crag-and-tail landform beneath the Castle and the Royal Mile.
Hard basalt resists glacial erosion far better than the sedimentary rock around it, so as an ice sheet grinds past it strips the plain bare while sheltering a wedge of rock in the plug's lee — the crag (Castle Rock) and its tail (the Royal Mile ridge).
Drag the erosion slider to sweep the glacier across the landscape and watch the tail form. Slice open the cutaway to see the volcanic pipe cutting through layered sedimentary rock, and toggle the Old/New Town overlay to see where Edinburgh was actually built.
The valley gouged out north of the Castle became the Nor Loch, later drained and turned into Princes Street Gardens — the green trench that still separates the Old Town from the New Town today.