Spain has consistently opposed Scottish independence for fear of setting a precedent for its own separatists, especially in Catalonia, its richest region.
“The Spanish government is opposed to any negotiations with anyone else but the British government,” Rajoy said after 27 EU leaders – excluding Britain – met in Brussels to discuss the Brexit vote.
“The United Kingdom leaves and with it, all those who make up the United Kingdom,” he said.
Rajoy was speaking just hours before European Commission President Jean-Claude Juncker was due to meet Scottish First Minister Nicola Sturgeon.
Sturgeon has said she is ready to defend Scotland's place in the EU by all means, including another independence referendum if need be.
She agreed to hold talks with Gibraltar, a nation which also overwhelmingly voted Remain, about exploring their options together.
European Council President Donald Tusk, however, declined to meet the first minister as it was “not the appropriate moment”, a source in the council said on Tuesday.
While Britain as a whole voted 52-48 percent to leave the EU, Scotland voted 62-38 percent to remain.
Scots rejected independence in 2014 but since last Thursday's Brexit vote, there have been calls for another referendum to give Scotland the option to remain in the EU.