Sudan quickly declared victory, saying its armed forces had "liberated" the area by force as thousands of people poured onto the streets of Khartoum cheering, dancing, honking car horns and waving flags.
South Sudan's seizure of the territory had raised the prospect of two sovereign African states waging war against each other openly for the first time since Ethiopia fought newly independent Eritrea in 1998-2000.
Tensions have been rising since South Sudan split away from Sudan as an independent country in July, under the terms of a 2005 settlement, taking with it most of the country's known oil reserves.
The countries are still at loggerheads over the position of their shared border and other disputes have already halted nearly all the oil production that underpins both economies.
South Sudan's President Salva Kiir ordered the withdrawal of his troops within three days, the country's information minister told reporters in the southern capital Juba.
"The Republic of South Sudan announces that SPLA (southern army) troops have been ordered to withdraw from Panthou (Heglig)," Barnaba Marial Benjamin said.
Benjamin said the withdrawal was in response to appeals from world leaders and "to create an environment for the resumption of dialogue with Sudan". U.N. Secretary-General Ban Ki-moon called on Thursday for the South to pull out.
The South still believed Heglig, which many southerners call Panthou, was its territory and wanted its status to be determined by international arbitration, Benjamin added.
In Sudan, where television channels broadcast military-themed montages, the defence minister said the "armed forces were able to liberate Heglig and clear it of South Sudan's forces and mercenaries".
"The armed forces moved ... to preserve what is left of the oil facilities," said Abdel Raheem Muhammad Hussein in a speech broadcast on state television.
Heglig is central to Sudan's economy because, before the attack, it produced about half of the 115,000 barrel-per-day oil output that remained in the country after the South seceded.
CELEBRATIONS
The announcement of Heglig's recovery set off widespread celebrations in the Sudanese capital, where the initial news that South Sudan had seized the territory had come as a major shock to many.
At a rally attended by thousands, Sudan's President Bashir praised the country's armed forces for what he described as a great victory.
"The president of the SPLM said they withdrew from Heglig. There was no withdrawal. We went into Heglig after we beat them, and we went in by force. Even now, they are running," he said.
"They started the war and we declare when it ends."
Tensions had been fuelled by a bitter dispute over how much the landlocked South should pay to export oil via pipelines and other infrastructure in Sudan.
Juba shut down its roughly 350,000 barrel-a-day ouput in January, accusing Sudan of seizing some of its crude. Oil accounted for about 98 percent of the South's state revenues.
In a speech later on Friday, Bashir further dampened hopes the two would reach a deal in the near future, saying Sudan's oil pipelines were now closed to the South's crude exports.
"We don't want fees from the oil of South Sudan and we will not open the pipeline. There is no oil from South Sudan that will pass through our pure land, so that not one dollar goes to these criminals," he said.
Rahamatalla Mohamed Osman, Sudan's undersecretary for foreign affairs, said the two sides were still fighting in some border areas. "The war has not ended," he told reporters.
Cars blared their horns as they rode down Khartoum's streets. People hung out of windows waving Sudanese flags and shouting "Allahu akbar", or "God is greatest". Some called for Kiir's downfall.
"This is the happiest day of our lives, the happiest day. Allahu akbar!" said one man with a flag wrapped around his neck as he sat on top of a white sedan.
World powers had urged South Sudan to pull out of Heglig following the incursion, which U.N. chief Ban Ki-moon described as an infringement of Sudan's sovereignty.
In a sign the latest flare-up in tensions may have a lingering economic impact, South Sudan's army spokesman Philip Aguer said Sudan had bombarded Heglig on Thursday, causing the central processing facility to catch fire.
Sudan's state information minister Sana Hamad in turn accused South Sudan of "intentionally" damaging Heglig's facilities, including the main electricity station.
The south secured its independence in a referendum promised in the 2005 peace accord that ended decades of civil war between Khartoum and the south. Religion, ethnicity and oil fuelled the conflict that killed about 2 million people.
REUTERS