UPDATE 3-Spain to change the law to force banks to pay mortgage stamp duty

* Bank shares gain up to 3 pct after ruling

* Government will force banks to pay duty in new law

* Bank of Spain says loan tax no longer a risk after court ruling (Recasts story leading with the Government's plan to change law)

MADRID, Nov 7 (Reuters) - The Spanish government moved to counteract a court ruling that would have forced customers to pay mortgage stamp duty, pledging on Wednesday to swiftly pass a law to oblige banks to pay the tax instead.

The law will come in to effect on Friday once it is published in the Official Bulletin, Spain's Socialist Prime Minister Pedro Sanchez said.

On Tuesday, the Supreme Court said that customers and not banks had to pay the tax, reversing a ruling in mid-October in which the court upset the status quo by declaring that lenders were legally responsible for it.

On Wednesday, Prime Minister Sanchez said that a Royal Decree will be approved "so that Spaniards will never pay this tax again."

The surprise ruling by the Supreme Court on Tuesday prompted the government's anti-austerity party Podemos, the main partner of the minority PSOE government, and other consumer organisations to call for public demonstrations.

Asked about potential appeals before European courts, Sanchez said that customers were within their right to do so.

NOT BACKDATED

Spain bank shares temporarily came off earlier highs following the comments by the prime minister.

However, they then resumed gains after it became clear that the government's new law would not be applied retroactively.

The Supreme Court ruling spared banks from potentially having to pay back billions of euros to borrowers who for years have paid the tax themselves.

This could have cost them more than 15 billion euros ($17.2 billion) collectively, analysts and ratings agencies, such as Moody's and DBRS, estimated.

Regulation and financial stability director at the Bank of Spain, Jesus Saurina, on Wednesday said that because the ruling reversed the situation to before Oct. 18, it was no longer a risk for the Spanish banking sector.

At 1348 GMT shares in Banco Sabadell were up 2 percent, while Bankia was up around 1.3 percent. Caixabank rose 2.8 percent while Banco Santander, BBVA and Bankinter were all up around 2 percent.

Smaller lenders like Liberbank and Unicaja with a high exposure to Spain and with balance sheets mostly relying on mortgage lending, rose 4.5 percent and 1 percent, respectively.

"The Supreme Court's decision to maintain the ruling has removed a tail risk and restored the much-needed legal security for the banks to continue operating in the long-duration business of mortgage loans," the broker Alantra said in a note, adding however that litigation risks were "here to stay". (Additional reporting by Belen Carreno and Paul Day; Editing by Louise Heavens and Elaine Hardcastle)