The lawsuit against Ignacio Cembrero, an expert on Spain-Morocco relations who works for El Confidencial news website, was accepted by a court in Madrid, a spokesman for the courts said.
According to a copy of the complaint seen by AFP, Morocco is demanding that Cembrero withdraw his allegations and pay Morocco's legal costs.
"The Kingdom of Morocco is not involved in spying on Ignacio Cembrero nor on any other citizen" and "does not have the Pegasus program," the lawyer representing Rabat said.
Cembrero had also flagged the lawsuit on his Twitter account.
"Morocco is taking me to court for accusing it of spying with Pegasus," he tweeted on Monday.
"It's a political trial to curtail journalists' freedom of expression," he tweeted.
He has said it is "the fourth time" Morocco had sued him in Spain but the first time it had demanded he "retract" claims Rabat was responsible for spying with Pegasus software.
Last year, an investigation by 17 media organizations accused Morocco of using the Israeli-made spyware, which infiltrates mobile phones in order to extract data or to activate a camera or microphone to spy on their owners. Rabat has denied the allegations.
Morocco was singled out as one of the countries which had bought the program from Israel's NGO Group and whose intelligence services had used the spyware against journalists.
A vast investigation by more than 80 reporters in 10 countries revealed in July 2021 that Pegasus spyware had been used against at least 50,000 people around the world.
They included at least 180 journalists as well as human rights defenders, politicians and military top brass.
The information was first leaked to Amnesty International and French NGO Forbidden Stories who then shared it with more than a dozen media organizations.
According to Forbidden Stories, Cembrero's mobile was one of a number of phones singled out "for targeting" by Pegasus.