Secretary of State John F. Kerry apologized Monday for past discrimination against gay and lesbian people at the State Department, an unusual acknowledgment that members of the diplomatic corps were hounded out for their sexual orientation.

“In the past — as far back as the 1940s, but continuing for decades — the Department of State was among many public and private employers that discriminated against employees and job applicants on the basis of perceived sexual orientation,” Kerry wrote in a brief statement published on the department’s website.

“These actions were wrong then, just as they would be wrong today.”

The apology refers to the era before and during the anti-Communist investigations led by Sen. Joseph McCarthy. Thousands of gay people were targeted in some government agencies, including the State Department, on grounds that their sexual orientation made them targets for blackmail by foreign agents.

Known as the Lavender Scare, the policy led to the investigation or expulsion of employees based on speech, manner of dress, and the accusations of co-workers or others, according to a State Department history of the period. State’s account includes this awkward testimony by a department official, John Peurifoy, before a panel of the Senate Appropriations Committee in 1950.

“Peurifoy tried to avoid discussing the subject of homosexuals,” the history says. “While replying to a question on dismissals, Peurifoy noted that 91 employees in the ‘shady category’ had been dismissed since January 1, 1947. When pressed to define this category, Peurifoy alluded to ‘moral weakness,’ ” and eventually said the category referred to gay people.

The State Department later set up a panel called the M Unit to investigate employees who were suspected of being gay.

“We wish more of our LGBT colleagues targeted by discrimination were alive to see the progress our country has made,” the group GLIFAA, which represents lesbian, gay, bisexual and transgender employees at the State Department, said in a statement. “We celebrate their legacy by continuing to work for full equality for LGBT employees of foreign service agencies.”

The apology comes with a little more than two weeks left in the Obama administration, which generally gets good marks from gay rights organizations for its work to address discrimination both within the State Department and overseas. Kerry named the first special envoy for LGBT issues in 2015. The diplomat, Randy Berry, has traveled widely to promote gay rights.

The gay rights group Human Rights Campaign praised Kerry for what it said was a long overdue acknowledgment.

“Although it is not possible to undo the damage that was done decades ago, Secretary Kerry’s apology sets the right tone for the State Department as it enters a new and uncertain time in our country under a new administration,” said government affairs director David Stacy.

Although the most blatant discrimination faded after the McCarthy era, gay employees were still targeted for investigation and firing as late as the early 1990s, according to academic and other documentation cited by Sen. Benjamin L. Cardin (Md.) in an appeal to Kerry last year to address a “dark stain” on the department’s history.

Cardin, the senior Democrat on the Senate Foreign Relations Committee, said Monday that he plans to introduce legislation that would reinforce the apology and help victims of discrimination seek redress.