Facebook Inc. has filed a lawsuit against Israel’s NSO Group, alleging the cybersecurity company’s spyware infected the phones of some users after it was delivered through the WhatsApp messaging platform.

In the suit, Facebook, which owns WhatsApp, filed Tuesday in federal court in California, the company says that NSO Group’s software was behind a sophisticated campaign to hack the phones and messages of journalists, human-rights advocates and other targets chosen by its clients, which include foreign governments.

“Unable...