U.S. District Judge Larry Burns made the decision after U.S. Marshals dragged the man out of the courtroom because of an angry outburst.
As survivors of the deadly January attack looked on, Loughner lowered his head, raised it and said what sounded like "Thank you for the freak show. She died in front of me." His words were loud but mumbled, and it wasn't clear who he was talking about. He wore a khaki prison suit and sported bushy, reddish sideburns.
The decision means that Loughner will be sent to a federal facility for up to four months in a bid to restore his competency.
Wednesday's ruling comes after the 21-year-old spent five weeks in March and April at a federal facility in Missouri, where he was examined by two court-appointed mental health professionals.
Loughner has pleaded not guilty to federal charges stemming from the January shooting at a Tucson political event that injured Giffords and 12 others and killed six people.
The Associated Press contributed to this report.