Weeks of precarious talks finally led to Friday’s releases of captives. Here’s the inside story