Rajoy governed Spain from 2011 until he was ousted in June last year by a no-confidence vote over a corruption trial that hit his conservative Popular Party (PP).
He was in power when Catalonia's executive tried to secede in 2017.
The 63-year-old appeared in Madrid's Supreme Court as a highly-anticipated witness in the trial of 12 separatist leaders and activists -- nine of them facing rebellion charges -- for pushing the referendum in October 2017 and for a subsequent short-lived declaration of independence.
"I really deplore those images, I don't like them," he told the court about footage of police violence against voters.
But "if people hadn't been called to an illegal referendum and if decisions hadn't been taken that violated the law, neither you nor I nor anyone would have had to see the injuries suffered by some people and some members of the state security forces."
Earlier, he defiantly told the court that separatist leaders "were fully aware that... as long as Mariano Rajoy was prime minister, there would be no (legal) referendum to liquidate national sovereignty."
He described the attempt to break with Spain as "an exceptional situation", demonstrated by the fact that he had to impose direct rule on the semi-autonomous region for the first time in Spanish democracy.
Earlier, Catalonia's former separatist president Artur Mas criticised the decision by Madrid to send police to stop people from voting in the referendum.
Mas stepped down in January 2016 before the secession bid, but was close to those who led the attempt.
"I said (at the time): 'Do you really think that the state will be so unintelligent to do something that will harm it directly with regards to its image in the whole world?'," he told the court.
"I confess I was wrong because that was the option."
Critics accuse Rajoy of having fuelled pro-independence passions in Catalonia. Support for separatism in the wealthy northeastern region leapt from 10 percent of voters in 2010 to 47.5 percent in 2017 -- much of it under his mandate.
Even before he came to power, Rajoy had campaigned as opposition leader against a new, agreed status for the region that gave it extra powers and defined it as a "nation" within the Spanish state.
The Constitutional Court eventually overruled the nationhood claim, fuelling pro-independence passions.
"His appearance is very important because he headed" the country before, during and after Catalonia's secession bid, said Paloma Roman, politics professor at Madrid's Complutense University.
It also comes as political debate in Spain becomes increasingly virulent ahead of snap general elections on April 28 where the Catalan secession crisis is a burning issue.
Before Rajoy's appearance, Joan Botella, politics professor at the Autonomous University of Barcelona, had predicted he would "have to respond to the question: why did he never sit down to talk" to the separatists?
And indeed he did, retorting that "the most important proposal he made was to stop violating the constitution."
No longer in politics
Prosecutors are seeking seven to 25 years in jail for the 12 Catalan separatist leaders and activists, including the region's former vice-president Oriol Junqueras.
Their independence bid prompted Rajoy's government to sack the Catalan executive, dissolve the regional parliament, impose direct rule on the semi-autonomous region, and call snap local elections.
Several days later, Catalonia's then leader Carles Puigdemont fled Spain for Belgium along with several other colleagues. Other separatist leaders were put in pre-trial custody.
But in regional elections in December 2017, separatist parties once again won a majority in parliament.
Months later in June, Rajoy left politics "for good" after being ousted.