The judge in the trial of South African Paralympic athlete Oscar Pistorius has ordered that he should undergo a month-long mental evaluation.
She ruled that psychiatric evidence before the court could not replace "a proper inquiry" into his mental health.
Correspondents say the trial, which began in March, will almost certainly now face a lengthy delay.
The prosecution argued on Tuesday that psychiatric tests were essential and that he should be referred.
But the defence vigorously opposed the move.
Mr Pistorius denies intentionally killing his girlfriend Reeva Steenkamp on Valentine's Day last year.
He says he accidentally shot her through the toilet door in a state of panic, mistaking the 29-year-old model and law graduate for an intruder.
The BBC's Andrew Harding in court says that Judge Thokozile Masipa's decision caught almost everyone by surprise.
The trial has now been adjourned until Tuesday, when the judge will give the details behind her ruling which could mean the Olympic athlete is evaluated for a minimum of 30 days at a psychiatric faculty.
Mr Pistorius's uncle, Arnold, welcomed the judge's "thorough" ruling and said it gave the family "confidence" in the justice system.
Judge Masipa said that the criminal code stipulates that if an accused person is alleged not to be criminally responsible or is alleged to be mentally ill, he should be evaluated.
She said that such an evaluation would ensure that Mr Pistorius would get a fair trial, adding it would be "preferable" for him to undergo the evaluation as an outpatient.
Prosecution lawyer Gerrie Nel made the referral application on Tuesday after forensic psychiatrist Merryll Vorster said earlier that the double amputee was suffering from generalised anxiety disorder (Gad).
Mr Nel said that the disorder diagnosis left him with no choice but to apply for an evaluation because the condition might have affected Mr Pistorius' behaviour on the night he killed Ms Steenkamp.
Legal experts say that the case may well hinge on the judge's understanding of the athlete's state of mind when he pulled the trigger.
They say the prosecution is keen to show that the defence keeps changing its reasons why Mr Pistorius fired his gun - from putative self-defence, to accidental shooting, and now to something linked to his anxiety disorder.
There are no juries at trials in South Africa, so the athlete's fate will ultimately be decided by the judge, assisted by two assessors.
If found guilty of murder, Mr Pistorius could face life imprisonment. If he is acquitted of that charge, the court will consider an alternative charge of culpable homicide, for which he could receive about 15 years in prison.
Mr Pistorius said in his statement at the start of the trial that he woke in the early hours and walked on his stumps to the balcony, pulled in two fans, closed the sliding door and drew curtains. He said that shortly before he had spoken to Reeva, who was in bed beside him.
He said he rejected prosecution claims that a witness heard arguing coming from the house before the shooting.
2. Bathroom window×
Mr Pistorius said he heard the bathroom window sliding open and believed that an intruder, or intruders, had entered the bathroom through a window which was not fitted with burglar bars.
"Unbeknown to me, Reeva must have gone to the toilet in the bathroom at the time I brought in the fans," he said.
Mr Pistorius said he approached the bathroom armed with his firearm, to defend himself and his girlfriend, believing Ms Steenkamp was still in bed.
Both sides agree four bullets were fired. Ms Steenkamp was hit three times.
Mr Pistorius said he fired his weapon after hearing a noise in the toilet which he thought was the intruder coming out of the toilet to attack him and Ms Steenkamp.
He said he was in a fearful state, knowing he was on his stumps and unable to run away or properly defend himself.
Mr Pistorius said he rejected claims that he was on his prostheses when he shot at the door.
A witness told the trial she woke to hear a woman screaming and a man shouting for help. She said that after the screams she heard four shots.
Mr Pistorius said he went back to the bedroom after shooting at the toilet door, still shouting for Reeva. Lifting himself up onto the bed, he felt over to the right hand side of it and noticed Ms Steenkamp was not there.
Mr Pistorius said this was when he realised she could have been in the toilet.
5. Toilet door×
Mr Pistorius said he went back to the bathroom but the toilet was locked, so he returned to the bedroom, pulled on his prosthetic legs, turned on the lights before bashing in the toilet door with a cricket bat.
Forensics expert Johannes Vermeulen told the court that the height of the marks on the door caused by the cricket bat suggest Mr Pistorius was on his stumps at the time.
6. Emergency calls×
Mr Pistorius's defence team say he then called security at the gated housing complex and a private paramedic service before carrying Ms Steenkamp downstairs.
A security guard claimed it was the other way round, and he had called Mr Pistorius first after reports of gunfire. However, phone records shown to the court revealed Mr Pistorius called the estate manager at 3:19am, a minute later he called the ambulance service and at 3:21am he called estate security.
A minute later he received an incoming call - estate security calling him back.
According to police phone expert Francois Moller, Mr Pistorius called his friend Justin Divaris a short time later and just after 4:00am he called his brother Carl.