Loretta E. Lynch’s long wait to become U.S. attorney general ended Thursday, with the Senate voting 56 to 43 to confirm the veteran New York prosecutor five months after President Obama submitted her nomination to Congress.

Lynch is expected to be sworn in as the nation’s 83rd attorney general Monday, according to Justice Department officials not authorized to comment publicly.

Obama said in a statement that “America will be better off” with Lynch in charge of the Justice Department. “She will bring to bear her experience as a tough, independent, and well-respected prosecutor on key, bipartisan priorities like criminal justice reform,” he said.

Lynch is the first African American woman to be nominated for the post, which has taken on a higher-than-usual profile in the Obama administration ­because of the leading role the Justice Department has recently played in the debate over race and policing across the country.

For Lynch, the time between nomination and confirmation was the longest for an attorney general nominee in 31 years. In the end, the confirmation vote margin was wider than expected: Ten Republicans, including Majority Leader Mitch McConnell (Ky.), joined the Senate’s 44 Democrats and two independents in supporting Lynch. Forty-three senators, all Republicans, were opposed.

Outgoing Attorney General Eric H. Holder Jr., stepping down after more than six years, said Lynch would be “an outstanding attorney general, a dedicated guardian of the Constitution, and a devoted champion of all those whom the law protects and empowers.”

Getting the Senate to a final vote had been a slow and rancorous affair, with Obama deeming the spectacle “embarrassing” last week. It gave Democrats frequent opportunities to lambaste McConnell over a span of months, and right up until Thursday.

“I guess I was naive in thinking my Republican colleagues would treat Loretta Lynch with the dignity that she and her office deserve,” Minority Leader Harry M. Reid (Nev.) said on the Senate floor. “Perhaps my mistake was forgetting that for Republicans, this isn’t about Loretta Lynch. It’s about President Obama.”

Obama nominated Lynch, 55, in November to replace Holder. The Senate, then under Democratic control, did not act on the nomination, preferring to spend precious time in the lame-duck session on judicial appointments that party leaders believed would stall in a Republican-controlled Senate.

A Republican Senate, the thinking went, would not dawdle in confirming a replacement for Holder, a frequent target of Republican enmity. That proved not to be the case, especially after Lynch became entangled in a deep partisan rift over Obama’s immigration policy.

During questioning before the Senate Judiciary Committee in late January, Lynch said she believed Obama’s executive actions on immigration last year passed legal and constitutional muster, angering Republicans who considered them an overreach.

“Ms. Lynch has said flat-out that she supports those policies and is committed to defending them in court,” Sen. Jeff Sessions (R-Ala.) said Thursday. “So I think Congress has a real role here. We do not have to confirm someone to the highest law enforcement position in America if that someone is publicly committed to denigrating Congress.”

Sen. Ted Cruz (R-Tex.) said that, under Lynch, “We are sadly going to see more and more lawlessness, more recklessness, more abuse of power, more executive lawlessness.”

Cruz spoke against Lynch on the Senate floor Thursday morning but was the only senator to miss the final vote.

After the January hearing, it took nearly a month for the panel to advance Lynch’s nomination, and once it did, the nomination became caught up in an unrelated political dispute.

An otherwise noncontroversial bill to combat sex trafficking became stuck on the Senate floor after pro-abortion-rights Democrats objected to provisions that would extend existing federal restrictions on abortion funding to a new victims’ compensation fund. At that point, McConnell tried to force Democrats to accept the language by tying Lynch’s nomination to the anti-
trafficking bill’s passage.

The move incensed Democrats, some of whom spoke in racial terms about what they saw as shabby treatment of a pioneering black woman. Minority Whip Richard J. Durbin (Ill.), for instance, said Republicans had asked Lynch “to sit in the back of the bus” by Republicans, a reference to civil-rights icon Rosa Parks.

On Thursday, Sen. Claire ­McCaskill (D-Mo.) called the Republican opposition “disgusting” and “base politics at its ugliest.”

“It doesn’t matter if you’re qualified,” she said. “That makes no difference. We have a new test: You must disagree with the president who nominates you. . . . This defies common sense.”

Obama did not blame Lynch’s race for the delay, but he showed increasing exasperation as the delay mounted. “It’s gone too far,” he said last week. “Enough. Enough. Call Loretta Lynch for a vote. Get her confirmed. Put her in place. Let her do her job.”

The deadlock broke Monday when party leaders agreed to restrict the victims’ fund to non-medical purposes, making trafficking victims instead eligible for health care under an existing federal program already subject to abortion restrictions. The anti-trafficking bill passed Wednesday afternoon on a 99-to-0 vote.

As senators voted on final confirmation around 2 p.m., several African American female members of the House watched from the corner of the Senate floor. After the vote, they greeted ­McConnell in a Capitol hallway and thanked him for supporting Lynch.

Twenty Republicans supported a procedural move earlier Thursday to close debate and proceed to Lynch’s confirmation. But only half of them voted to confirm her in the final vote: Kelly Ayotte (N.H.), Thad Cochran (Miss.), Susan Collins (Maine), Jeff Flake (Ariz.), Lindsey O. Graham (S.C.), Orrin G. Hatch (Utah), Ron Johnson (Wis.), Mark Kirk (Ill.) and Rob Portman (Ohio). McConnell joined them after expressing reservations in the weeks leading up to the vote.

Rick Tyler, a spokesman for Cruz’s presidential campaign, noted Cruz had voted against closing debate and proceeding to a final vote earlier in the day, calling that “the vote that mattered.” Two other presidential candidates, Rand Paul (R-Ky.) and Marco Rubio (R-Fla.), voted no on both questions.

Thursday’s vote had more considerable implications for senators seeking reelection next year. Ayotte, Johnson, Kirk and Portman face reelection battles next year in states that voted for Obama in the 2012 presidential race. A fifth Republican seeking reelection in an Obama state, Patrick J. Toomey (Pa.), voted no, citing doubts about Lynch’s willingness to be an “independent legal check on executive overreach.”

A few Republicans waited until Thursday to announce their intentions.

Ayotte, for instance, said in a statement that Lynch was “clearly qualified and has the necessary experience” and had given “written assurance” that she would abide by court rulings on the immigration orders.

Johnson, in a statement, said that “elections matter and the president has the right to select members of his cabinet,” while Portman said that “new leadership is needed at the Justice ­Department.”

Sari Horwitz and Ed O’Keefe contributed to this report.