Anthony D. Weiner, the former Democratic congressman whose sexting scandals ended his political career and embroiled him in a tumultuous F.B.I. investigation of Hillary Clinton before the election, pleaded guilty to a felony on Friday, crying openly as he admitted to conduct that he knew was “as morally wrong as it was unlawful.”
The plea agreement ended a federal investigation into a series of sexually explicit pictures and messages that Mr. Weiner sent last year to a 15-year-old girl in North Carolina.
It capped the long, tortured downfall of Mr. Weiner, who ruined a once-promising career in Congress and then spoiled various attempts at resurrecting his reputation, all through his uncontrolled habit of using social media and texts to send explicit images to women.
“I engaged in obscene communications with this teenager,” Mr. Weiner said, his voice high and shaky, and his body trembling. Those communications “included sharing explicit images and encouraging her to engage in sexually explicit conduct,” just as he had done with adult women, he said.
Mr. Weiner, 52, will have to register as a sex offender where he works and lives, and he may face a prison term. He pleaded guilty to transferring obscene material to a minor, which carries a sentence of up to 10 years in prison.
Federal prosecutors said in the plea agreement that a sentence in the range of 21 to 27 months would be “fair and appropriate.” Mr. Weiner is to be sentenced on Sept. 8 in Federal District Court in Manhattan.
Mr. Weiner was led into the courtroom for the brief hearing wearing a slim navy suit with a white shirt and maroon tie; a wedding band could be seen on his left hand. He sat quietly by his lawyers, exchanging whispers with one, Arlo Devlin-Brown, and an occasional smile.
But Mr. Weiner quickly dissolved into tears as he read from a written statement when the judge, Loretta A. Preska, asked that he describe what he had done.
“I’ve given this some thought,” he told her.
As he paused to brace himself, Mr. Devlin-Brown placed a hand on his lower back to comfort him and a courtroom worker brought over a box of tissues.
Mr. Weiner told Judge Preska that from the time he was in Congress through the first half of last year, “I’ve compulsively sought attention from women who contacted me on social media, and I engaged with many of them in both sexual and nonsexual conversation.”
“These destructive impulses brought great devastation to my family and friends, and destroyed my life’s dream in public service,” he said. “Yet I remained in denial even as the world around me fell apart.”
Reports of the federal investigation surfaced in September after DailyMail.com reported that Mr. Weiner had engaged in an online relationship with the girl, which included explicit messages sent over social media and suggestive texts.
It was during the investigation that the F.B.I. seized Mr. Weiner’s electronic devices, including a laptop on which agents found a trove of emails to his estranged wife, Huma Abedin, a top aide to Mrs. Clinton. That discovery led to the surprise announcement in late October by James B. Comey, then the F.B.I. director, that the bureau was conducting a new investigation into Mrs. Clinton’s handling of official email, an inquiry that ended two days before the election, with no charges brought. Mrs. Clinton recently attributed her election loss in part to Mr. Comey’s announcement.
After the guilty plea, Joon H. Kim, the acting United States attorney in Manhattan, said, “Weiner’s conduct was not only reprehensible but a federal crime, one for which he is now convicted and will be sentenced.”
Mr. Devlin-Brown said that his client had “accepted full responsibility” for his conduct, and that the resolution was “on terms far less severe than could have been sought.” He said Mr. Weiner would have no further comment and “remains focused on his recovery.”
As Judge Preska informed Mr. Weiner of his rights during the hearing, he stood with his hands together before him, listening intently. When the judge asked if he was pleading guilty because he was guilty, he responded directly, “I am guilty, your honor.”
Mr. Weiner told the court that in January 2016, he was contacted by — and began exchanging online messages with — “a stranger who said that she was a high school student, and who I understood to be 15 years old.”
“I knew this was as morally wrong as it was unlawful,” he said.
Mr. Weiner, choking up and rubbing his forehead, said that last fall he “came to grips for the first time with the depths of my sickness.”
“I — I had hit bottom,” he said. “Through treatment I found the courage to take a moral inventory of my defects.” He said he began a program of recovery and mental health treatment that he was continuing.
“I accept full responsibility for my conduct,” he told Judge Preska. “I have a sickness, but I do not have an excuse.”
Mr. Weiner said he had apologized to those he had hurt, including the teenage girl “whom I mistreated so badly, and I am committed to making amends to all those I have harmed.”
Mr. Weiner was forced to resign from Congress, where he represented parts of Queens and Brooklyn, in June 2011, not long after an explicit picture, sent from his Twitter account, became public. Mr. Weiner initially claimed that his account had been hacked but eventually admitted that he had lied and that he had sent the image and had inappropriate online exchanges with at least six other women.
An effort to resurrect his career progressed in 2013 as he began his candidacy for mayor of New York. But that, too, collapsed after the emergence of additional explicit online messages.
Mr. Weiner’s marriage to Ms. Abedin fell apart after new suggestive text messages surfaced in August, including one with an image of Mr. Weiner’s crotch as he lay next to the couple’s son, who was 4.
After the hearing, Mr. Weiner silently left the courthouse with his lawyers, ignoring a crowd of reporters and photographers as he stepped into a black Nissan and was driven away.