The two communities in the Niger Delta - the Ogale and Bille - claim decades of oil spills have ruined their homes.
They wanted their case heard in the UK.
But the High Court in London agreed with the Anglo-Dutch company's argument that the case, affecting more than 40,000 people, should be heard by local courts in Nigeria.
The villagers have repeatedly said they will not get a fair hearing in Nigeria.
However, Igo Weli, a spokesman for the multinational's subsidiary, the Shell Petroleum Development Company of Nigeria (SPDC), told the BBC it was a "myth" that the communities could not get justice in their home country while welcoming the High Court decision as "common sense".
"It's about claims by Nigerians about the operations of a Nigerian company in Nigeria and I think the Nigerian court is the best place to handle that," he said.
"It's about incidents related to sabotage, illegal refining and crude thefts. Bille and Ogale are two communities that have been severely impacted by those activities which is a major source of pollution in the Niger Delta."
But neither of the communities - who say repeated spills since 1989 have meant they do not have clean drinking water, farmland or rivers - are ready to give up.
King Emere Godwin Bebe Okpabi, ruler of the Ogale, said: "Our community is disappointed but not discouraged by this judgement.
"This decision has to be appealed, not just for Ogale but for many other people in the Niger Delta who will be shut out if this decision is allowed to stand.
"Shell is simply being asked to clean up its oil and to compensate the communities it has devastated."
They have been given the go-ahead by Mr Justice Fraser to challenge his ruling in the Court of Appeal.
In 2014, another community in the delta, Bodo, took Shell to court in the UK over an oil spill. That case was settled by Shell the following year with an unprecedented $84m (Â£55m) payout to the Bodo community.
The difference with this latest case is that the Nigerian subsidiary SPDC has refused to submit to a UK jurisdiction.
A British court has blocked Nigerian villagers' attempt to sue oil giant Shell for allegedly polluting their fishing waters and farmland.