Rajoy appeared as a witness in a long-running graft trial that has rocked his conservative People's Party and hurt him at the ballot box. He denied any knowledge of an illegal financing scheme within his party during the trial.
Spanish Prime Minister Mariano Rajoy on Wednesday denied any knowledge of an illegal financing scheme within his conservative People's Party (PP) and said he not taken any illicit payments, as he testified in court in a long-running corruption case.
Rajoy is the first sitting premier in Spain to be called to court as a witness.
The Gurtel corruption trial centres on graft allegations involving businesses and small city councils, but it has tainted the PP and led to allegations of a slush fund within the party.
Asked in court whether he knew if there was an illegal financing scheme for election campaigns within the party and cash donations from anonymous donors, a combative Rajoy said, "Never."
"I never heard anything because, as I have stated, I was never in charge of financial matters within the party," Rajoy said.
About 30 protesters demonstrated noisily behind yellow metal barriers as the car bringing Rajoy to court pulled into the garage in San Fernando de Henares.
They blew whistles and held up banners that read "Justice" and "Make the PP illegal."
It is the first time that a serving prime minister has testified in a criminal case.
Two former premiers – Felipe Gonzalez, an ex-Socialist leader, and Adolfo Suarez, prime minister during Spain's transition to democracy –appeared in court in separate trials but they were no longer in office.
Rajoy, 62, had asked to testify by video-conference, saying he was too busy to appear in person, but Spain's High Court ordered him to come.
Rajoy is not accused of anything himself but was asked to testify because as the party's chief since 2004 – and a high-ranking member before that – he would have knowledge of how the PP operated.
The prime minister did not sit in the traditional witness stand in front of the judges, but instead on the elevated platform where they preside.
He testily answered questions about his roles in the party posed by a lawyer from the ADADE lawyers' association, which is a party in the case and is close to the main opposition Socialists.
The court appearance is a major embarrassment for Rajoy, who has sought to distance himself from a series of corruption scandals that have for years hounded his party, in power since 2011.
The affair and other graft scandals contributed to the PP losing its absolute majority in 2015 elections.
Analysts said Rajoy's court appearance would remind voters of the corruption scandals which are preventing the party from reaping the benefits the improving Spanish economy.
Opposition parties have seized on Rajoy's court appearance to renew their attacks on the government over its handling of corruption, which polls repeatedly show is a top voter concern.
Rajoy will not only answer questions in court, "he is also going to submit to a polygraph test in the form of the gaze of 46 million Spaniards," said Pedro Sanchez on Saturday, head of Spain's main opposition Socialists.
Altogether, 37 defendants face justice in the case including two former party treasurers and businessman Francisco Correa, the alleged head of the network.
Companies would allegedly give him a commission of two to three percent on the value of public contracts.
Correa said in court that he would then give politicians involved in awarding contracts some of the money.
His main accomplice was allegedly Luis Barcenas, a former PP manager and treasurer who was once close to Rajoy.
In a separate branch of the case Barcenas is accused of setting up a slush fund to top up the salaries of PP leaders. He has said he made payments to top PP members.