A group of female students from Afghanistan are finally getting their wish of coming to the United States for a robotics competition.

The girls' applications for US visas had been denied twice, but the White House said President Donald Trump intervened and they will be allowed in to participate in the competition.

White House spokeswoman Sarah Huckabee Sanders confirmed details of the reversal on Wednesday, ending a saga that had sparked international backlash.

Homeland Security Department spokesman David Lapan said the US Citizenship and Immigration Services approved a State Department request for six girls from the war-torn country to be allowed in, along with their chaperone, so they can participate in the competition.

The charity organising the competition celebrated the reversal in a jubilant statement.

"I truly believe our greatest power is the power to convene nations, to bring people together in the pursuit of a common goal and prove that our similarities greatly outweigh our differences," said Joe Sestak, president of First Global.

He credited "the professional leadership of the US State Department" for ensuring that all 163 teams from 157 countries, including a team of Syrian refugees, would be able to participate.

The US State Department had declined to comment on why the Afghan team's visa applications were denied, saying that "all visa applications are adjudicated on a case-by-case basis in accordance with US law".

A senior administration official said that Mr Trump raised the issue with his national security adviser HR McMaster during his trip to Germany last week for the G20 summit, and had asked for additional options.

The State Department and Department of Homeland Security came up with several - including the idea of "paroling" the girls through the Department of Homeland Security, which was the option ultimately chosen by the National Security Council.

Parole is a temporary status in which a person who is otherwise ineligible to enter the country is allowed in temporarily because of an emergency or humanitarian purpose, or because it is deemed to be in the public good.

Without the reversal, which was first reported by Politico, the girls would have had to watch via video link from their home town in western Afghanistan.

The girls wanted to show the world that Afghans could also construct a hand-made robot and they had been deeply disappointed by the initial rejections.

"When we heard that we were rejected we lost hope," said 14-year-old Sumaya Farooqi.

"We applied again for the US visa and we were rejected again."

Farooqi and her teammates faced seemingly insurmountable obstacles to even get to a point where they could seek permission to attend.

It took them six months to prepare, often working seven days a week, as they constructed a robot that sorts balls, has the ability to recognise orange and blue colours, and can move objects to put them in their correct places.

The girls travelled from their homes in Herat after convincing family members to let them go - no small feat in a country where young girls are often discouraged from pursuing academic study, especially in hard sciences such as maths.

They made the 800-kilometre (500-mile) journey to the US Embassy in Kabul twice because their applications were denied the first time, even though that location was targeted by a deadly lorry bomb on May 31 in which more than 150 people were killed and more than 400 others wounded.

War-torn Afghanistan has faced a series of large-scale attacks as the Taliban stepped up its war against the Kabul government in this year's summer offensive.

In addition, the emerging Islamic State group affiliate in Afghanistan has tried to increase its footprint with attacks in urban areas.

Afghanistan is not part of Mr Trump's order to temporarily ban travel from six Muslim-majority countries.

Teams from Syria, Iran and Sudan - which are on that list - were granted visas to compete.

Members of the team from Gambia were also granted visas after initially being denied.

First Global, a not-for-profit charity, holds the annual international robotics challenge in hopes of sparking a passion for science and technology among high school students around the world.

It is an Olympics-style competition in which one team from every nation is invited to participate.