The origins of Montemayor date back to 2,000 years BC, according to the remains of bell-shaped pottery and Iberian sculptures.
During the Roman occupation the village was known as, Ulia and the, Bellum Hispaniensis of Aulus Hircio tells us that Ulia was the only city of Betica that remained faithful to Julius Caesar during the Civil War. In 46 B.C, Gnaeus Pompey ‘El Joven’, besieged Ulia while his Brother, Sextus, settled in Corduba.
When Cesar arrived in Hispania, in aid of the town, he sent an army formed by around 5,000 men, under the leadership of, Lucius Vibius Paciecus, while he marched towards Corduba. Cesear defeated the last remnants of opposition in the Battle of Munda in 17 March 45 BC. This was the final battle of Caesar's civil war against the leaders of the Optimates. With the military victory and the deaths of Titus Labienus and Gnaeus Pompeius (eldest son of Pompey), Caesar was politically able to return in triumph to Rome, and then govern as the elected Roman dictator.
The decline of the Empire would also affect the city, of which there is little news in the following centuries. Visigothic domination accentuates the decline of the city.
During the Moorish period the city was named, Ulyat Kanbaniya, and was one of the fifteen agricultural districts in which the Cora or province of Córdoba was divided.
In the year 1233, Fernando III sent troops to occupy the place, which was almost uninhabited and dilapidated, and as from this moment was called, Montemayor. After the conquest of Córdoba, the territory becomes part of the domain of Fernán Núñez de Témez.
During the first third of the fourteenth century the Nasrid King, Muhammed IV of Granada, carried out a series of raids along the border. The town was repopulated in 1340 by, Martín Alfonso of Córdoba ‘El Bueno’, with the permission of, King Alfonso XI, and was granted permission to build a Castle, as his nearby Castle of Dos Hermanas was insecure and unsuitable for constant defence against the Moors.