Harvard University filed a federal lawsuit against the Trump administration Monday, arguing it has violated the university’s constitutional rights by freezing billions of dollars in federal funding and imperiling its academic independence.

The suit sets up a legal showdown between America’s most prominent university and the U.S. president, who has been on an escalating campaign to reorder elite higher education.

“The consequences of the government’s overreach will be severe and long-lasting,” Harvard President Alan Garber said in a community message announcing the lawsuit. Research at risk by the funding cuts, Garber said, includes work on child cancer, infectious disease outbreaks, and easing the pain of soldiers wounded in battle.

Harvard argues in the suit that the government has cut off funds “as part of its pressure campaign” to force the university “to submit to the government’s control over its academic programs.”

The government’s actions flout the First Amendment by seeking to control what university faculty may teach and whom the school may hire, Harvard says. It also argues that the federal government is disregarding laws and regulations around how to pursue civil-rights investigations.

The suit asks the court to halt the funding freeze and declare as illegal both the freeze and the demands asked of the university.

In recent weeks, a new government task force has shaken top American universities, pausing or freezing billions of dollars in federal grants and contracts at premier institutions such as Columbia and Harvard, and putting many schools on high alert.

The task force says it is targeting schools that failed to adequately protect Jewish students during pro-Palestinian protests that disrupted campuses last year. It is also seeking to push Harvard to incorporate a broader diversity of ideas on its campus.

White House spokesman Harrison Fields said Monday in response to the lawsuit: “Taxpayer funds are a privilege, and Harvard fails to meet the basic conditions required to access that privilege.”

In the suit, Harvard argues the government “may not interfere with private actors’ speech to advance its own vision of ideological balance.”

Critics of the task force, including Harvard’s president, have said antisemitism is being used as a cudgel to give the Trump administration more control of universities.

Harvard is the first university to sue the administration over actions by President Trump’s antisemitism task force, though faculty groups at Harvard and Columbia have filed lawsuits, accusing the administration of exploiting civil-rights laws to undermine academic freedom and free speech.

The confrontation with Harvard began in late March after the government said it was reviewing nearly $9 billion in federal funding at the school and asked it to take certain actions “necessary for Harvard University’s continued financial relationship with the United States government.”

The task force believed Harvard would concede, just as Columbia had, according to someone familiar with the negotiations. But when Harvard got a list of demands from the task force in an April 11 letter, the university was shocked. The list included requirements that Harvard allow federal-government oversight of admissions, hiring and the ideology of students and staff. The school rejected the administration’s proposal and struck a defiant tone.

The clash quickly escalated . The Trump administration said it would freeze more than $2 billion in multiyear grants and contracts. It has also threatened the school’s tax-exempt status and ability to enroll foreign students, and launched a probe into its foreign funding.

The 50-page lawsuit makes the case for why the federal government should continue to partner with universities on scientific research, a collaboration that dates back to World War II. “Millions of Americans are healthier and safer as a result” of such partnerships, and they have led to breakthroughs at Harvard such as developing drugs to battle Parkinson’s and Alzheimer’s and supporting astronauts in space, the lawsuit says.

Harvard argues that the administration has taken an improper shortcut in the usual process for handling antisemitism concerns under Title VI of the Civil Rights Act, freezing funding—a typically rare outcome—before doing a full investigation.

The government also can’t “identify any rational connection” between antisemitism concerns and the research it has frozen, the university argues.

Harvard acknowledges in the suit that tensions on campus rose to dangerous levels over the Israel-Hamas war. “Members of the Jewish and Israeli communities at Harvard reported treatment that was vicious and reprehensible,” Harvard’s lawyers wrote.

In response, the university said it has changed policies to improve campus safety, enacted discipline and enhanced programs “designed to address bias and promote ideological diversity and civil discourse.” Some on campus have said Harvard leaders haven’t gone far enough to curtail antisemitic behavior.

The lawsuit names as defendants agencies and cabinet members from across the Trump administration, including the departments of justice, health and human services, education, energy and defense, and the General Services Administration. Officials from several of those agencies sit on the antisemitism task force.

Write to Douglas Belkin at Doug.Belkin@wsj.com and Sara Randazzo at sara.randazzo@wsj.com