Multi-phase deal to end fighting and release captives struck after months of negotiations