A mentally ill man who has been in and out of psychiatric confinement over the past decade was charged Tuesday with raping a 10-year-old girl he followed into a Minneapolis apartment building.

Ahmed H. Abdi, of Minneapolis, was charged in Hennepin County District Court with two counts of first-degree criminal sexual conduct and remains jailed in lieu of $500,000 bail. He has a court hearing scheduled for early next month.

Abdi has a long and sometimes violent history in connection with mental illness, according to court records.

On Aug. 6, the court ordered Abdi back to the University of Minnesota Medical Center for treatment. A week later, a letter from U Medical Center clinical treatment coordinator Anna Burke to the county attorney's office endorsed Abdi being provisionally discharged again, writing that he "has endorsed readiness for discharge, no longer meets the criteria for inpatient hospitalization and is in agreement with the discharge plan."

Burke declined Wednesday to address Abdi's case and was seeking to have someone with the U Medical Center comment. Abdi's attorneys were not available for comment.

Abdi was committed last fall, and in May, the Hennepin County attorney had his commitment extended to Nov. 13. But according to the county attorney's office, any provisional discharge would be between the patient, the facility and his case manager.

According to court documents, the terms of the provisional discharge would remain in effect even after he was released from the hospital.

On Sunday, people outside the Riverside Plaza complex on the 1600 block of S. 4th Street flagged down police officers after they heard screams from the third-floor hall and saw a young girl crying.

According to the criminal complaint, the emotionally shaken girl told officers that a man followed her into an elevator, got out at the same time on the third floor and went with her to the east hallway. He then reportedly asked the girl what her name was and extended his hand to her. When she shook his hand, he refused to let go and ordered her not to move. He then raped her, the complaint said.

Relying on the girl's description of her attacker, police spotted the man on surveillance video entering the elevator with the girl and determined his identity. Authorities located Abdi in St. Paul and booked him into jail early Monday.

Police on Wednesday confirmed that the girl did not know her attacker. Sexual assaults are carried out far more often by people who know their victims than by strangers.

The November 2014 civil commitment order in Hennepin County District Court said that Abdi was a schizophrenic who "engages in grossly disturbed behavior … and he poses a substantial likelihood of causing physical harm."

Abdi also has a history of substance abuse dating to 2003.

On Nov. 14, 2014, Abdi was hospitalized at the U Medical Center after police found him brandishing a knife at a stranger on the street, according to the commitment order.

During that hospitalization, he "demonstrated poor sexual boundaries with female hospital staff," attempted to leave the hospital without permission, refused to take his medication and engaged in "intense staring at staff and other residents."

The commitment lasted until January, when Abdi was provisionally discharged four months early. But on Aug. 6, the provisional discharge was revoked by the court and ordered to remain active until at least mid-November because Abdi had grabbed a woman and had pulled a knife on another person. Abdi's case manager also noted his use of marijuana and refusal to take his medications.

Staff writer Mary Lynn Smith contributed to this report.

Paul Walsh • 612-673-4482