It was the first time that a serving prime minister in Spain has testified in a criminal case.
The trial centres on a vast kickback scheme that allegedly saw companiesshower former PP lawmakers and civil servants with bribes in exchange for contracts and helped fund the party.
Rajoy is not accused of anything himself but was asked to testify becauseas the party's chief since 2004 -- and a high-ranking member before that – he would have knowledge of how the PP operated.
"I never dealt with party economic affairs," Rajoy told the court duringthe proceedings broadcast live on Spanish television.
He said there was a "clear separation" between political and economic partymatters within the PP and that his responsibilities were always "of the political sort".
Dozens of protesters blew whistles and held banners reading "Make the PP illegal" as Rajoy arrived by car at the National Court compound in San Hernando de Henares on the outskirts of Madrid.
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.
He did not sit in the traditional witness stand in front of the judges, but instead answered questions -- sometimes testily -- from the elevated platform where the three judges sat.
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 are no longer in office.
Rajoy arrives in court in San Fernando de Henares. Photo: AFP
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 onthe 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.
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, including Rajoy.
During his testimony on Wednesday, Rajoy reiterated that he never met Correa and he dismissed the allegations of secret payments to PP officials as "absolutely false".
Asked in court whether he knew if there was an illegal financing scheme within the party or cash donations from anonymous donors, Rajoy said: "Never."
The court appearance was a major embarrassment for Rajoy, who has sought to distance himself from a series of corruption scandals that have hounded the party and contributed to the PP losing its absolute majority in the 2015 election.
A sticker reading, "illegalize the Popular Party: The mafia Party", is pictured on a post as media wait for Spanish Prime Minister Mariano Rajoy to leave after declaring as a witness. Photo: AFP
Calls for resignation
Analysts said corruption scandals are preventing the PP from reaping the benefits of the strengthening Spanish economy.
"Corruption issues will continue to put a ceiling on the PP's electoral aspirations," Antonio Barroso, deputy director of research at Teneo Intelligence, said in a note.
"While Rajoy should be benefiting from the strong economic rebound, the ruling party has been losing support in the polls recently."
Opposition parties seized on the court appearance to renew their calls for Rajoy to resign.
"I think today it is more obvious than ever that we have to remove the PP from power," said Pablo Iglesias, the leader of far-left party Podemos which has made the fight against graft a centrepiece of its platform.
The leader of the main opposition Socialists, Pedro Sanchez, accused Rajoy of "hiding in a fog of ignorance".
"For dignity, not yours but of democracy, I demand on the part of the Socialists that you resign," he added.
After his court appearance, Rajoy visited the PP's headquarters in central Madrid where he was greeted with applause.
"I replied with what I knew, I am glad to have cooperated with the court," he said.