Later on, Castille was threatened by the Almoravides, a nation of African Moors. The Moorish kings already settled in Spain had many bitter quarrels amongst themselves; there was trouble and treason all over the land. Yahia, who was protected by the Cid, and called himself his friend, was murdered by a wicked alcayde named Abeniaf soon after he had joined with Ruy Diaz to defend Spain against the Almoravides. Abeniaf buried the treasures of the murdered king, and let some of the new invaders into Valencia, for which service they made him Wali, or governor of the city.

The Cid came with a great army of Christians and Moors, and lay siege to Valencia, so incensed was he at the cruel death of Yahia, and began by attacking the suburbs, because by gaining them he could close all round the city, and prevent the Moors from going in or coming out. That siege of Valencia was very terrible, the people died daily of hunger; they eat horses, dogs, cats, and mice, and when all the flesh was gone they had only a little wheat and garlic, and a few raisins and figs.

In their sore need they implored some more of the Almoravides to come to their aid, although a great quarrel had broken out between that people and Abeniaf. The Almoravides set out for Valencia, but they were dismayed by a violent tempest which arose and turned back. Then the besieged went almost mad through hunger and misery, and the Cid came nearer its walls, thinking that famine would force them to yield. The longer the siege lasted, the more defiant did Abeniaf become; besides which he governed the people very cruelly, and oppressed them in every way. The Cid was very cruel too outside their walls, and showed them no mercy. He sent word to them that he would burn all persons who should dare to come out of the city, and it is said that several Moors who tried to escape were burned by his command. Many men, women, and children, too, came out whenever the gates were opened, and sold themselves to the Christians for food. The price of a Moor was a loaf and a pitcher of wine.

At last Abeniaf agreed to deliver up the place if no succour came within fifteen days, provided he might still continue in his office of Wali. The people thought they might yet be saved, because they had entreated the King of Saragossa to assist them, but no help came, and the gates were opened, and the Christians poured in to the city.

The Cid entered with all the hidalgos and knights, and went up to the highest tower in the wall, whence he could look down on the whole of Valencia; and the Moors came to him, and they kissed his hands, and bade him welcome. The Cid, in return, ordered that all the windows of the tower which looked towards the streets should be closed, that the Spaniards might not annoy the Moors by prying into their affairs, and commanded the Christians to guard the people and to pay them the greatest honour. The Moors were very grateful for his kindness, and rejoiced indeed that the city had been given up, for now the provision merchants could come inside the gates and they could buy food; and some of them were so famished that they went and plucked the grass and herbs from the field, and tried thus to satisfy their sharp hunger. It must have been a sad sight to have seen those who had survived the famine standing about like ghosts, whilst there was mourning in every house, and space had not been found to bury all the dead.

The Cid planted his banner on the Alcazar, which was the name given to all royal houses and palaces in Spain. He caused Abeniaf to be seized by force, and after he had made him say where he had concealed the treasures of Yahia he condemned him to be burnt alive, but showed mercy to his son when the Moors entreated him not to include him in the punishment of his father; and had the Cid put the innocent child to death it would have been as dreadful a crime as Abeniaf was guilty of in murdering Yahia.

The city of Valencia lay in a great plain which was called the Garden, because it was covered with the most luxuriant vegetation, and trees, such as the mulberry, olive, orange, carob, and palm grew in its fertile soil. There were fair gardens lying between the walls and the shore.

When the Cid had taken up his abode in the vast and beautiful Alcazar, the people began to cast off their sorrow and gloom, and to take part in the rejoicings made by the Spaniards. Valencia was now all his own. He suffered the Moors to remain in the city and to keep all their herds and flocks; they were to give him a tenth part of their substance, and to retain all their customs; and he made a good man Wali over them that they might be governed by their own laws. Those who were not content with this arrangement, he ordered to go and dwell in the suburb of Alcudia, outside the walls. From this time he was called the Cid Campeador, the latter title being given to one greatly renowned for his exploits.

One day, Hieronymo, a holy and learned man, "all shaven and shorn," came from the East to Valencia, and desired to see the Cid. He said that if he might once meet the Moors on the battle-field, and have his fill of smiting them, he would be content. These were warlike words for a priest, but they pleased Ruy Diaz, and the very next day after the stranger arrived the mosques were changed into churches, and Hieronymo was made Bishop of Valencia.

The King of Seville soon came with the Almoravides to besiege the Cid in his new abode. Ruy Diaz defeated him, and won from them his famed horse Bavieca, although the chronicles say that Bavieca was the horse he chose when a boy, because it was so fiery, and the name was given to it from his godfather exclaiming, "Bavieca (meaning simpleton) thou hast chosen ill."