Commanders and units can retreat from a battle, either as part of a scripted plan, or from routing, e.g. due to heavy losses or fear effects. Retreating units try to reach the map edge, at which point they disappear from the battle. Any units that make it off the battlefield before the battle ends will try to retreat to an adjacent province.
Each commander gets a high chance to make a safe smart retreat. A smart retreat goes into a fort if there is one in the same province, and if not, to an adjacent friendly province. A commander who does not make a smart retreat goes to a random adjacent province, which could be hostile, in which case the commander is lost immediately.
To make a smart retreat, a commander passes a morale check against 10. If the check fails, the commander gets a 50% chance anyway. If the commander has the appropriate terrain survival, they will get second 50% chance for a smart retreat. This means a pretender, with 30 morale, has a 99.92% chance to make a smart retreat. Most commanders have around 80% chance.
Troops have to pass a morale check to follow their commander's retreat. The squad morale bonus counts double for this, undisciplined units suffer a -3 penalty, and the squad morale penalty for skirmish formation also applies. If the morale check fails, units have a 50% chance to make a smart retreat on their own, and 50% chance to retreat randomly. If the unit has the appropriate terrain survival, their chances are 75% to make a smart retreat.
this page could use a flowchart that bug regarding retreating across unfrozen rivers?