Islamic State affiliates in Libya briefly took over the security headquarters in the western city of Sabratha, killing and beheading 12 security officers before being driven out early Wednesday morning, two city security officials said.

The incident highlighted the enduring presence and unpredictable striking power of the local IS militants in this strategic city which serves as a hub for migrants heading to Europe.

Taher...