Two of Libya’s most powerful rival militias have maintained pragmatic truces in a joint effort to beat back the advance of extremist group Islamic State, commanders and political representatives said.

The truces have been centered in the country’s west, where armed groups supporting two competing governments have been engaged in fierce conflict for more than a year. The fight has effectively split the country and stirred concerns of a full-blown civil war.

The...