After Caesar met with Cleopatra and detained her brother, the young King Ptolemy XIII, Ptolemy's regent Pothinus colluded with the Egyptian general Achillas to expel Caesar. Achillas and 20,000 men marched on Alexandria where Caesar waited with only 4,000 men. Caesar had little choice but to secure what he could and prepare for a siege. He maintained control of the palace and the nearby harbor, while Achillas took control of the surrounding city. Hard street fighting ensued with neither side being able to gain an advantage.
In late October, mid summer by season, the Egyptians attempted to secure the port and cut off Caesar's supply and potential escape. After bitter fighting, however, Caesar's men won the day, burning the Egyptian ships and securing the harbor. Unfortunately though, the fire spread into the city and damaged the Great Library, taking with it many unrecoverable documents. Nevertheless, Caesar discovered the involvement of Pothinus with the opposition and had him put to death, but Cleopatra's younger sister, Arsinoe IV managed to escape the palace and join the Egyptian army. She, however, had a mind of her own and had little use for her brother's general Achillas. She ordered him executed in turn and turned over command of the Egyptian forces to her servant Ganymedes.
Caesar was still hemmed in and ordered Pompey's former legion, under the command of Calvinus, to join him in Egypt with all haste. Ganymedes cut off the supply of fresh water to the Roman section of the town, and the legionaries were near panic. Caesar calmed them by having them dig fresh water wells, but the situation was still precarious. When Calvinus finally arrived, Caesar assembled his fleet and sailed out to meet the Egyptians. Winning a small engagement between about 30 ships on each side, it led him to attack the island fortress of Pharos. The situation was hotly contested and could've gone either way. At one point when the Romans were forced to retreat from land to their docked ships, Caesar's own galley was sunk and he had to swim 200 paces to another nearby ship to reach safety. Rather than be disheartened, however, the incident seemed to anger the legionaries and they continued fighting with renewed resolve. It was the Egyptians who, despite gaining some momentum, realized that the Romans would not quit, and they temporarily withdrew to re-assess.
In early December, the Egyptians had convinced Caesar that they were tired of fighting and that, Arsinoe and Ganymedes were doing them no good. If Caesar would only release Ptolemy, they would submit to Caesar and live peacefully under Roman 'protection.' Ptolemy too played the part, convincing Caesar that he didn't want to leave his presence at all. With artificial tears, or actually described after the fact as tears of joy by the disgruntled Romans, Ptolemy left Caesar's palace camp and rejoined his own army. However, once there, he entertained no thought of capitulating and resumed the war in earnest.
Meanwhile, Caesar was waiting for more troops to arrive, under Mithridates of Pergamum. He had moved to Cilicia and Syria to recruit fresh armies, which were sorely needed to pressure the Egyptian rear. The situation in Alexandria was a virtual stalemate with neither side able to gain an upper hand. Ptolemy continued to pressure Caesar by threatening his lines of supply, and another naval battle ensued. A Roman navy under the command of Tiberius Nero (father of future emperor Tiberius) sailed out to meet a combined Rhodian and Egyptian fleet. In late January 47 BC, the two fleets met and the Rhodian admiral, Euphranor was killed, securing Caesar's supply for at least the time being.
Early in 47 BC, Mithridates' army was ready and he began the march from Syria towards the Nile. Ptolemy's army marched away from Alexandria to meet the threat, and Caesar soon followed. Near Pelusium, the final battle for control of Egypt took place. Mithridates captured the town and Ptolemy returned the favor by surrounding it and laying siege. In late March of 47 BC, the combined armies of Caesar and Mithridates laid waste to the Egyptians. In the complete rout that ensued, Ptolemy attempted to flee by ship on the Nile, but the mayhem overtook him. His panicked men overloaded the escape vessel, sinking it, and taking the life of King Ptolemy XIII with it. After the battle, Caesar marched back to Alexandria where the city simply surrendered, making him the undisputed master of Egypt.
Caesar secured the reign of Cleopatra by enforcing the will of her father Ptolemy XII, and married her to her younger brother Ptolemy XIV. Even so, by this time, Cleopatra was at least a couple of months pregnant. Arsinoe IV, the only surviving enemy in the Egyptian royal family was banished to be kept for Caesar's later triumph. Over the next several months, Caesar and Cleopatra went on what seemed like a honeymoon vacation along the Nile. Traveling on Cleopatra's barge as far south as his men would let him, they toured the entire country all the way to the border of Ethiopia. The relationship between the two is significant. In order to spend time with her, Caesar blew off his remaining opposition in Africa, under Cato, Labienus and Scipio, and in Spain against the sons of Pompey. It's certain that cementing her, and thereby himself, in a position of power in Egypt would be beneficial to Rome politically and economically, but it certainly seemed to be more than simple political manipulation. Cleopatra was the living goddess of Egypt and Caesar, by now, likely began to see himself as a truly diving Roman god. Though his Roman dignity prevented him ever divorcing his wife Calpurnia, and making any sort of legal arrangement with Cleopatra, this divine connection likely had a profound effect. Though Caesar, by the nature of Roman law, could never recognize the son of Cleopatra, Caesarion as his own, and even the ancient sources handle it carefully, it makes little sense that the boy belonged to anyone but Caesar.
While Caesar and Cleopatra enjoyed their love affair in earnest, however, Republican forces in Spain and Africa continued to be a threat. Making matters worse, though, Pharnaces II of Pontus, son of the great Roman enemy Mithridates the Great (no relation to Caesar's ally of the same name, though he did serve the great king as a youth), was making incursions against neighboring provinces in the Roman east. Once again Caesar gathered his forces and marched off to face another threat.