Microsoft, of Redmond, Wash., will pay $21 million of the total, US Attorney Catherine Hanaway said in St. Louis.
The companies were accused of accepting payments from businesses advertising illegal online gambling from 1997 to 2007. None admitted liability, Hanaway said.
Google, Yahoo, and Microsoft, which combined handle more than 90 percent of US Internet searches, are looking to expand online advertising sales by letting marketers target consumers with ads that match their interests.
Through acquisitions and internal development, the companies are building technology to deliver ads on mobile phones, video sites like YouTube, Internet television, and video game consoles.
Yahoo, based in Sunnyvale, Calif., will pay $7.5 million, the second-largest sum, Hanaway said.
Google, the world's most-used search engine, is to pay $3 million.
Part of the companies' payments, $13.5 million, will be spent on advertising aimed at people under age 22, informing them that online gambling is illegal in the United States, Hanaway said.
"Although Microsoft stopped accepting ads from sites associated with online gambling nearly four years ago, this agreement reflects our ongoing commitment to online safety," company spokesman David Bowermaster said. "We're pleased to have reached a mutually beneficial outcome with the US attorney's office in St. Louis that will provide substantial resources to protect consumers from harmful Internet content."
"Yahoo stopped accepting online gambling advertisements years ago," spokeswoman Kelley Benander said in an e-mailed statement. "After the US attorney's office contacted Yahoo with its concerns, we worked cooperatively over several years to reach this settlement."
Jon Murchinson, a spokesman for Mountain View, Calif.-based Google, said the ads appeared as sponsored links on Google's Web pages.
"Google voluntarily discontinued running such ads, which were a very small part of our AdWords business, in April 2004," he said by e-mail.
Of Microsoft's $21 million share of the fines, $9 million will be spent on the public service advertising, $4.5 million will be paid to the government, and the balance will be given to the International Center for Missing and Exploited Children to assist it in its mission, Hanaway said.
Yahoo will spend $4.5 million on the ads over three years and will forfeit $3 million to the government, according to the prosecutor's statement.
The entire Google payment will be made to the government, according to the parties' settlement agreement.
Hanaway's office led the prosecution of Betonsports PLC, once a publicly traded Internet gambling business in London that pleaded guilty to racketeering conspiracy earlier this year then filed for bankruptcy in the United Kingdom.
Twelve people associated with the company were indicted last year on charges related to what prosecutors called Betonsports's violation of US laws barring the placing of bets by telephone wire across state lines. All pleaded not guilty.