Barjawan allied himself with the Turkish commander called, Manjutagin, who himself was a great force in Syria. He readily espoused to Barjawan's faction, and formed an alliance with some of the Bedouin chiefs and left Damascus at the head of six thousand troops to march towards Egypt. Ibn Ammar mobilized his troops under the leadership of Suleman bin Falah and provided him with the large sums of money to be used in diverting the loyalty of the Bedouin chiefs against Manjutagin. The two armies clashed between Ramla and Askalan, and after three days of minor encounters, they fought the final battle. Manjutagin was subdued and taken prisoner and sent captive to Cairo. The battle resulted in victory for the Maghriba, but impugned a dangerous problem to the state, a fast growing opposition between the Maghriba and Mashriqa in Egypt. The defeated Mashriqa arrived in Cairo and threatened Ibn Ammar's rule, while the majority of Maghribawere in Syria with Suleman bin Falah. To overcome the problem, Ibn Ammar planned to increase his supporters and at the same time adopted a moderate line of policy towards Mashriqa, and pardoned Manjutagin. Suleman bin Falah also followed a similar policy in Syria and tried to convince its inhabitants that his plans were for peace and security. He dismissed Jaysh ibn Samsama from the governorship of Tripoli and replaced him with his own brother Ali.
Thus, Jaysh, a powerful Katama chief, went to Cairo to revenge himself by attempting to overthrow Ibn Ammar. He made an alliance with Barjawan and the chiefs of Mashriqa. Barjawan's opportunity to gain power came with the presence of Jaysh in Egypt. He provoked riots and disturbances in Cairo and threw the blames on Ibn Ammar and his supporters. Ibn Ammar invited them to his palace under the pretext to discuss the riots between Berbers and Turks, but secretly had planned their executions. However, Barjawan, who had planted many spies in Ibn Ammar's palace, was informed of this and formed a counterplan. He and his supporters decided to accept the invitation. They planned to foil the attack by retreating among them, thus exposing Ibn Ammar's treasonable intentions. Barjawan's plan succeeded and he and his allies returned to the royal palace, declared Ibn Ammar to be a traitor and prepared to fight. With as many supporters as he could muster, Ibn Ammar left Cairo and camped in the desert. Barjawan followed him and in a battle which lasted half a day, Ibn Ammar was defeated, and fled. By the overthrow of Ibn Ammar in 387/997, Barjawan assumed the office of wasita (chief minister) after Ibn Ammar had held office for a little less than eleven months. Barjawan took out al-Hakim in public to demonstrate his loyalty towards the Fatimids.
Barjawan pardoned Ibn Ammar and granted him the same monthly allowances and supplies that he had received during the period of Imam al- Aziz.