Mystery solved.

Former Deputy Attorney General Rod Rosenstein has ‘fessed up to giving explosive text messages of FBI employees Peter Strzok and Lisa Page to the press in 2017.

The messages between the two, exchanged in 2016 while both were involved in sensitive political probes, revealed their antipathy to then-candidate Donald Trump and loyalty to Hillary Clinton.

Rosenstein’s admission came in a Friday-night court filing by the Department of Justice, which is seeking to dismiss Strzok’s lawsuit challenging his June 2016 firing, Politico reported.

The former agent’s case seeks damages for invasion of privacy, arguing that the texts were disclosed due to political pressure from the White House.

But Rosenstein, who left the DOJ last year, says he made the texts public to protect Page and Strzok — because Congress was about to hear about the embarrassing messages anyway.

“Providing the most egregious messages in one package would avoid the additional harm of prolonged selective disclosures” from leaky congressional staffers, wrote Rosenstein, who now has a corporate law gig.

The texts showed that Page and Strzok had feared Trump might win the election.

Both had worked on the probe into whether Clinton jeopardized classified information by using a private email server while she was secretary of state as well as Crossfire Hurricane, the feds’ investigation into the Trump campaign.

Later, they worked briefly on special counsel Robert Mueller’s probe into alleged ties between Trump’s campaign and Russia.

“This man cannot be president,” Page wrote in March 2016.

“She just has to win now,” she said in a July 2016 message, referring to Clinton.

In his texts to Page, Strzok referred to Trump as an “idiot” and a “douche.”

Shortly before the 2016 election, he wrote that the prospect of a Trump presidency made him “scared for our organization.”

The president has called Strzok a “sick loser,” “a fraud,” “incompetent” and “corrupt,” and praised his firing.

Trump also made Strzok’s extramarital fling with Page a running topic of scorn during his MAGA rallies.