The Trump administration and the sheriff in Portland, Ore., agree on this much: The crimes were hideous and preventable. But they are fiercely blaming each other for allowing them to happen.

The case has stirred up a tempest because the man accused of crawling through the bedroom window of a 65-year-old woman, tying her up and raping her is an illegal immigrant, Sergio Jose Martinez, who has been deported to Mexico 20 times and has been arrested 10 times this year.

Under political fire for releasing Mr. Martinez after his most recent stint in jail, the sheriff declared that President Trump’s immigration agency was at fault for failing to provide a legal basis for holding him longer, while the White House blamed the sheriff for letting him go.

Cases like that of Mr. Martinez around the country are inciting outrage, amplified by an administration that is eager to expel undocumented immigrants.

In a recent speech in Miami that made national headlines, Attorney General Jeff Sessions described the case and several others like it in graphic detail, and said that releasing potentially deportable immigrants like Mr. Martinez when their jail terms were over does “far broader damage to the country than many understand.”

“At its root,” he added, “it is a rejection of our immigration laws.”

The political jockeying is rooted in a vexing constitutional disconnect between criminal justice and immigration enforcement, which is a matter of civil, not criminal, law. Questions about the legality of trying to connect the two may become one of the most significant legal challenges to the Trump administration’s immigration policy, experts say.

The way the administration sees it, the nation’s jails have the potential to become a powerful pipeline for deportation. But jailed immigrants must serve their sentences first, before they can be detained by the immigration authorities — and as a practical matter, it is often impossible for immigration agents to be present and ready at the jail the moment the immigrants are released, which may happen on short notice, late at night or on a weekend.

The immigration agency has tried to solve that problem by requesting that the local police keep holding inmates like Mr. Martinez for days after their sentences end, to allow time for agents to pick them up. In a series of decisions, federal judges have declared the practice unconstitutional — but the Trump administration has continued to demand compliance anyway.

Since he took office, the president and his attorney general have mounted an unbridled political assault against local officials who refuse to honor the requests, which are known as detainers. At the same time, though, cities and states across the country have adopted policies that bar local officials from doing so. The situation has local sheriffs and police chiefs in a bind: Whom should they obey, the president or the courts?

“Until the Supreme Court speaks on this, you will continue to see a division in the country,” said Muzaffar Chishti, the director of the Migration Policy Institute at the New York University School of Law.

Making matters worse, both sides of the argument have tended to mischaracterize the underlying legal issues in order to score political points.

In his frequent public scoldings of local officials, Mr. Sessions routinely ignores the constitutional issues that the detainer requests raise. Judges have said that holding people in criminal custody for a civil infraction violates the Fourth Amendment protection against unreasonable seizures, and the administration’s demands violate the 10th Amendment by forcing states to do the federal government’s bidding.

The Multnomah County sheriff, Mike Reese, whose jurisdiction includes Portland, has responded to criticism over the Martinez case with another problematic argument, one that has been frequently advanced by officials under fire from the administration.

In a statement, Sheriff Reese said he could not have held Mr. Martinez after his sentence ended because immigration officials had not produced a valid criminal warrant. “Instead, they processed a civil detainer, which they know cannot be legally used in Oregon,” Sheriff Reese wrote.

Under federal law, though, immigration officials have no standing to obtain a warrant in the criminal courts.

While the legal issues remain unresolved, cases like that of Mr. Martinez continue to pile up, and to be trumpeted by the administration with unrelenting pressure.

Nery Israel Estrada Margos, who had previously been deported to Guatemala, turned himself in to the police in Santa Rosa, Calif., for killing his girlfriend about two weeks after he was released on bail awaiting trial for battery.

The local sheriff in Santa Rosa told enraged residents that he was legally bound to release Mr. Estrada after he posted bail. But a spokesman for Immigration and Customs Enforcement, James Schwab, faulted the sheriff, saying that his policy concerning detainers had “clearly failed in this case, as it has in too many others.”

“Not only does the policy undermine ICE’s ability to carry out its mission, but it needlessly puts our communities at risk,” Mr. Schwab said.

Advocacy groups for immigrants are campaigning to strengthen the legal firewall between jails and immigration enforcement with legislation to restrict extended jail holds or ban them altogether. At least four state legislatures and many more cities and counties have considered enacting such laws this year.

The advocates say that when the local police cooperate too closely with the immigration agency, undocumented immigrants avoid coming forward to report crimes or act as witnesses.

“It’s not about those individuals,” Thomas A. Saenz, president and general counsel of the Mexican American Legal Defense and Educational Fund, said of defendants like Mr. Martinez and Mr. Estrada. “It’s about the message that’s sent to the community.”

Mr. Saenz and others say that while the extreme cases capture public attention, most of the people for whom the immigration agency issues detainers were arrested for low-level offenses like driving without a license — charges that they say hardly justify throwing aside the Bill of Rights.

For many sheriffs, though, the worst stories can be too jarring, both ethically and politically, to ignore. Most sheriffs are elected, and the cases often prompt indignant letter-writing campaigns and explosive town-hall-style meetings, with constituents on both sides of the debate demanding answers.

“They’re mortified,” Jonathan F. Thompson, executive director of the National Sheriffs’ Association, said of his group’s members. “They know that the people they are trying to keep off the street, they are unable to.”

More than a dozen people who were held on detainers have brought lawsuits against local governments, and the vast majority of those suits have been successful, resulting in settlement payments as high as $145,000. Even so, some sheriffs honor detainer requests despite the risk of litigation.

“When they take a chance and they hold them, it can boomerang and be very damaging,” Mr. Thompson said.

The Trump administration has turned up its use of detainers, issuing about 11,000 a month, or nearly 80 percent more than last year, and is playing a more active role in defending their use.

Mr. Sessions made his recent speech on the issue in Miami-Dade County, which is currently being sued for honoring a detainer, and held the county up as a positive example for other local governments. The Justice Department has filed legal briefs supporting the county, and has asked to present oral arguments in court.

In addition to the administration’s public criticism of local officials who do not comply with detainers, Mr. Trump has threatened to withhold federal grant money from jurisdictions that refuse to comply with them.

State governments have split on the issue. Texas recently enacted a law that requires local law enforcement officials to comply with detainers, or face a $25,000-a-day fine and removal from office. (It has not yet taken effect because of litigation in federal court.) In Illinois, on the other hand, the governor has promised to sign a bill that would severely restrict extended jail holds, and in California, which already limits them, there is legislation pending to restrict them further.

Sheriffs from across the political spectrum have called on the federal government to settle the issue definitively, which would probably require an overhaul of the country’s immigration laws, an unlikely undertaking in the current political climate. In the meantime, local governments are left with administrative moves by the executive branch that have widely been perceived as cosmetic and ineffectual.

For example, in April, the immigration agency changed its standard form for detainers to replace the word “request” at the top with “demand.” It also announced that each request would be accompanied by an administrative warrant, a type not reviewed by a judge. Experts say neither step would appear to make any legal difference.

A meeting this spring between Mr. Sessions and several sheriffs offered one reason the Trump administration may seem so far out of sync with local authorities on the issue. According to one sheriff who was there — Richard Stanek of Hennepin County, Minn. — when the federal court decisions from the last three years concerning extended jail holds came up in discussion, Mr. Sessions appeared to be unfamiliar with them.

“He was still living in 2014,” Mr. Stanek said. “He had no idea what we were talking about.”