All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
There is no contradiction because the Ahaadeeth that prohibit going out are specific to the plague only according to the view of some scholars, and according to them, this ruling does not apply to other diseases. Some other scholars interpreted it to refer to all infectious diseases that can be transmitted from a sick person to a healthy person. The ‘Uraniyyoon who were mentioned in the Hadeeth were not affected with an infectious disease; rather, they were suffering Istisqaa' (abdominal edema), as in the narration by Imaam Muslim. The Hadeeth reads (that they said): “The climate of Madeenah does not suit us so our stomachs have become bloated and our bodies have become weak…” There was no epidemic in Madeenah at all at that time. For more benefit, please refer to Fataawa 85932 and 240034.
Also, the prohibition does not include going out of the country where there is war because of fearing to be killed or harmed, as fearing to be killed and warding harm off oneself is required. What is forbidden is for the warriors to flee without any valid reason when they meet the enemy. The Quran ordered the Muslims to be firm when they meet the enemy and not to flee, and the Prophet, sallallaahu ‘alayhi wa sallam, considered fleeing on the day when the Muslim army marches to meet the enemy a grave major sin.
Allaah Knows best.