The daily fantasy sports companies DraftKings and FanDuel have agreed to merge after a turbulent year in which both of their values plummeted as several attorneys general questioned the legality of their games in their states.

The merger must be approved by regulators and will be time-consuming — the companies said they expected the deal to close in the second half of 2017. Until then, both sides will operate under their own brands.

The merger was one of necessity: Lobbying and legal costs had damaged both companies’ bottom lines to the extent that representatives of the companies last month asked the New York attorney general’s office to allow them to pay a combined $12 million settlement in installments after claims that they employed false and deceptive advertising practices, two people familiar with those negotiations said.

In recent weeks, according to these two people, FanDuel, based in New York, laid off more than 60 people, and both companies have acknowledged that they are months behind in their payments to vendors, especially to the array of public relations and lobbying firms that they have employed across the nation to persuade individual state legislatures to legalize daily fantasy games — the most critical component of rebuilding their business.

"Joining forces will allow us to truly realize the potential of our vision, and as a combined company we will be able to accelerate the pace of innovation and bring a richer experience to our customers than we ever could have done separately,” Jason Robins, chief executive of DraftKings, said in a statement.

Employees of FanDuel worked at their office in New York City in the fall of 2015. Credit Michael Nagle for The New York Times

If the merger is approved, Robins will retain that title in a new company, while FanDuel’s chief executive, Nigel Eccles, will become chairman of the board.

"While both companies have accomplished much already,” Eccles said, "this transaction will create a business that can offer a greater variety of offerings, appealing to new users, including the tens of millions of season-long fantasy players that haven’t yet tried our products.”

Last year, at the beginning of the N.F.L. season, DraftKings and FanDuel overwhelmed sports broadcasts with hundreds of millions of dollars in advertising that emphasized their get-rich-quick prize payoffs. At the time, it was a largely unregulated, multibillion-dollar industry in which players paid a fee on a website, assembled virtual rosters of players in pursuit of jackpots ranging from $22 to $2 million, and scored points based on the real-world outcomes of professional games.

Daily fantasy sports appeared to be a virtual cash machine. The companies were valued at over $1 billion each. Their investors included Major League Baseball and the N.B.A.; the Dallas Cowboys owner Jerry Jones and the New England Patriots owner Robert K. Kraft; and major media companies like NBC.

After a DraftKings employee won a major jackpot on FanDuel’s site in October 2015, however, Eric T. Schneiderman, the New York State attorney general, began an inquiry into whether employees of the companies had used inside information to prey on customers on each other’s sites. Soon, scores of class-action lawsuits were filed in courts across the country.

Schneiderman shut down the industry in New York, declaring daily fantasy sports to be illegal gambling, but laid the groundwork for a deal in March when he suggested a June 30 deadline for the State Legislature to act to address the games’ legal status. It did, and Gov. Andrew M. Cuomo signed the bill into law in August.

New York was the eighth state to declare daily fantasy sports legal, and the companies are facing expensive efforts to have their games legalized in the big markets of Illinois and Texas.