Petrobras, Brazil's state oil company, has agreed to pay more than $853m (£650m) to the US and Brazil, ending a long-running corruption investigation.

The probe stemmed from a bribery scheme at the firm, which involved millions in payments that were concealed from investors and regulators.

The payments were facilitated by executives at the "highest levels", including the firm's board, the US Department of Justice said.

Brazil is to receive 80% of the money.

Authorities in the US - where Petrobras is listed on the stock exchange - agreed not to prosecute in exchange for the remainder of the funds.

"Those who choose to access our capital markets while failing to disclose the corrupt activities of company executives will be held accountable," said US Attorney Zachary Terwilliger.

The payments were uncovered as part of Brazilian money laundering investigation at a petrol station, before widening to uncover corruption involving some of the country's top politicians and biggest companies, including Petrobras and Odebrecht.

According to the Petrobras settlement's official account of the scandal, former executives funnelled payments from companies seeking business with the state to politicians, in some cases accepting part of the bribe themselves.

The payments were often concealed through fictitious costs or consultancy agreements, it says.

The full scale of the scheme, which took place between at least 2004 and 2012, is estimated at $2bn, of which more than $1bn went to politicians and political parties, the account says.

In a statement, Petrobras stressed that the agreement found that the company was also a victim of the scheme, which involved former executives, some of whom are now serving prison sentences in Brazil.

It said: "The agreements serve the best interests of Petrobras and its shareholders and put an end to the uncertainties, burdens and costs associated with potential litigation in the United States."

Under an order from the Securities and Exchange Commission, Petrobras has agreed to return $933m in gains stemming from the false financial statements.

That will be credited toward a $2.95bn settlement reached in a class-action suit brought by investors in New York.