In an unusual flexing of diplomatic muscle, President Trump lashed out on Thursday at the prime minister of Sweden over what he said was the unfair treatment of the rapper ASAP Rocky, who was charged with assault earlier in the day after nearly three weeks in Swedish custody.

“Give A$AP Rocky his FREEDOM,” Mr. Trump wrote on Twitter, further inflaming the international incident that has grown out of the musician’s arrest early this month. “We do so much for Sweden but it doesn’t seem to work the other way around. Sweden should focus on its real crime problem!”

Give A$AP Rocky his FREEDOM. We do so much for Sweden but it doesn’t seem to work the other way around. Sweden should focus on its real crime problem! #FreeRocky

Mr. Trumpadded that he was “very disappointed in Prime Minister Stefan Lofven for being unable to act.”

Rocky was charged on Thursday with having committed an assault causing actual bodily harm on June 30 in central Stockholm, Swedish prosecutors said in a statement, capping weeks of speculation about how the country would handle a street brawl that was captured, at least in part, on camera.

The rapper will remain in custody, at least until his trial begins on Tuesday, the Swedish Prosecution Authority said in a statement.

The punishment for Rocky could include a fine based on his daily earnings or a maximum of two years in prison, said Annika Collin, a spokeswoman for the Swedish Prosecution Authority.

The decision to charge Rocky, and Mr. Trump’s increasing ire over the charges, will sharpen the focus on a case that started as a fistfight, but has ballooned into much more, with Sweden facing accusations of racism and human rights abuses for its treatment of the rapper.

Rocky, 30, whose real name is Rakim Mayers, is accused of assaulting Mustafa Jafari in Stockholm after an altercation in the street that was captured on video. The rapper and two other men were detained on July 5 as prosecutors investigated the matter.

Slobodan Jovicic, Rocky’s lawyer, said in a telephone interview on Thursday that the rapper acted in self-defense. On July 2, Rocky posted two videos on Instagram that Mr. Jovicic said show Mr. Jafari and another man following and harassing the rapper, despite being repeatedly asked to stop. In one of his tweets on Thursday, President Trump referred to the videos, “watched the tapes of A$AP Rocky, and he was being followed and harassed by troublemakers.” He also used the hashtag #FreeRocky.

But Daniel Suneson, the public prosecutor for Stockholm, said Mr. Jafari had been punched, kicked and struck with a glass bottle in an attack that lasted several minutes. One widely watched video shows Rocky throwing Mr. Jafari to the ground and, along with two members of his entourage who also will face trial, punching and kicking Mr. Jafari.

Mr. Suneson said he had even more evidence against the rapper, including footage from CCTV cameras and witness statements.

Rocky’s case was initially treated as a minor incident, but rapidly escalated after the rapper was detained and tour dates were canceled. Pop stars including Justin Bieber shared a Change.org petition created by Rocky’s team that demanded his release. More than 600,000 people have signed.

Mr. Trump said on Twitter at the time that he had told the prime minister he would “personally vouch” for Rocky’s bail if the rapper were to be released. Sweden has no system of bail, and usually keeps foreigners accused of crimes in detention because of the flight risk.

There was no response from the Swedish embassy in Washington Thursday evening following Mr. Trump’s latest tweets. But Mr. Lofven’s office said in a statement on Saturday thathe would not intervene, and that “in Sweden everyone is equal before the law.”

“If it walk like a duck and it quack like a duck, then it’s a duck,” she added.

Lovette Jallow, a human-rights activist in Sweden and commentator on race issues, said in a telephone interview on Tuesday that “racism in Sweden is very problematic.” “But it is not a case of racism in the ASAP Rocky case,” she added.

The man did hit the bodyguard, the statement added, but in self-defense.

Mr. Jovicic, Rocky’s lawyer, said the rapper was “under a dark cloud,” but added that he and his legal team had “good hope that we will get a result, and get him home soon.”

He refused to consider the possibility that Rocky might be jailed for a lengthy period. “Let’s not talk about that now,” he said. “We are preparing for a trial.”

Alex Marshall is a European culture reporter, based in London. @alexmarshall81

Joe Coscarelli is a culture reporter with a focus on pop music. His work seeks to pull back the curtain on how hit songs and emerging artists are discovered, made and marketed. He previously worked at New York magazine and The Village Voice. @joecoscarelli