The ruling delays the execution until at least 1 June 2027, the date by which Hasan must file a legal petition contesting his punishment, the judge ruled.
District Judge Anthony Mattivi halted the execution of Hasan, 56, so he can challenge his death sentence. Hasan had been scheduled to die on 3 December.
Mattivi, in his order pausing the execution, gave Hasan until 1 June to file a habeas petition contesting the death sentence.
The execution, set for Fort Hood, where the shooting occurred, would have been the first by a U.S. military firing squad since 1945. It also would have been the first U.S. military execution in more than 60 years.
Hasan, a U.S.-born Muslim of Palestinian descent, was sentenced to death by a military jury in 2013 for carrying out the Al-Qaeda inspired attack.
He has admitted to the shooting. In 2009 he opened fire in a crowded medical building where soldiers preparing to deploy were waiting for check-ups and vaccines.
He shot and killed 13 unarmed soldiers. In addition to those killed, 32 others were injured. The attack ended when officers shot Hasan in the back, paralyzing him from the chest down.
Hasan is being held at the military prison at Fort Leavenworth in Kansas. He had asked the Kansas district court to delay his execution while he appealed it on constitutional grounds.
U.S. military law requires that a military execution be approved by the president. Trump approved Hasan's execution in October.
Lawyers for the government had argued in court on Monday that the timing was too close to Hasan's execution for him to file his petition. Prosecutors filed an immediate appeal of the judge's ruling to a circuit court.
The last U.S. military execution was carried out in 1961, when John Bennett was hanged for rape and attempted murder.