The name probably comes from agar, a salt pan, after the brackish soil where salt was once made by evaporation. The recorded story starts around the 11th century. In 1504 or 1505 Sultan Sikandar Lodi rebuilt the city on the east bank of the Yamuna and made it his seat of government. He also founded Sikandra, now a suburb on the road to Delhi.
Agra became the Mughal capital in 1526. Babur did not like what he found: his memoir calls the land around it ugly and detestable. He built a garden anyway, the Aram Bagh, now called Ram Bagh, 2.3 km north of the Taj. Sher Shah Suri broke Mughal rule from 1540, and Akbar won the city back in 1556.
Akbar built the city on the right bank, where most of Agra still lies today, and began the fort in 1565. It took about eight years. A traveller of the time judged Agra and Fatehpur Sikri each much bigger than London, and described the road between them as one long market.
Jahangir finished Akbar's tomb and added the tomb of Itmad-ud-Daula. His son Shah Jahan took building to its peak. Work on the Taj began in 1632, a year after Mumtaz Mahal died, and went on until at least 1647. It strained the empire's resources and caused a mini-famine around Agra.
In 1658 Shah Jahan was shut up in the fort by his son Aurangzeb, and the court moved to Delhi. The old emperor died there in 1666. Later Jat conquerors took the silver doors of the Taj and melted them down, and in 1803 the British East India Company took the city.
In 1857 the uprising reached Agra. Some 6,000 Europeans and others took shelter inside the fort on 3 July, and J.R. Colvin, the province's lieutenant-governor, died inside the fort and was later laid to rest before the Diwan-i-Am. In 1858 the capital of the North-Western Provinces moved to Allahabad.
After independence Agra joined Uttar Pradesh but lost out on the state capital, which went to Lucknow. The Taj and the fort became World Heritage Sites in 1983. Tourism now drives the city: the Agra area drew upwards of 9.5 million visitors in 2019. The air is bad enough that a zone of strict industrial limits was drawn around the Taj after a 1996 Supreme Court ruling.