By Farouk Chothia and Danai Nesta Kupemba and Barbra Plett-Usher in Cape Town, BBC News
South Africa's governing African National Congress (ANC) and the main opposition Democratic Alliance (DA) have agreed to form a government of national unity, along with two smaller opposition parties.
This follows weeks of speculation about who the ANC would partner with after losing its parliamentary majority for the first time in 30 years, in last month's elections.
It got 40 percent of the vote, while the DA came second with 22 percent.
The agreement paves the way for ANC leader Cyril Ramaphosa to remain president.
The newly elected MPs will elect a president later on Friday, as the National Assembly meets for the first time since the 29 May elections.
They will meet in a convention centre in Cape Town because the parliament complex was damaged by fire several years ago.
ANC secretary-general Fikile Mbalula says the coalition deal is on track and that the agreement with opposition parties is a "remarkable step".
"We are engaging beyond this sitting on what needs to be done to constitute the government of national unity. Our work doesn't stop," he said.
An alliance between the centre-right DA and the ANC is unprecedented as the two parties have been rivals for decades.
The ANC led by Nelson Mandela led the campaign against the racist system of apartheid in 1994 and won the country's first democratic elections.
The DA's critics have accused it of trying to protect the economic privileges the country's white minority built up during apartheid - a charge the party denies.
DA leader John Steenhuisen said the deal was a "new chapter in our history".
He told journalists that in return for the DA's support of Ramaphosa, the ANC would back the DA's Annelie Lotriet as deputy speaker of parliament.
He also said the power-sharing arrangement would involve cabinet positions for the DA which has until now been an opposition party.
The deal also includes the Inkatha Freedom Party (IFP), a conservative party with a strong Zulu base, which got 4 percent of the vote, and the Patriotic Alliance (PA), which draws support from the coloured community, as people of mixed-race are known in South Africa.
South African news website News24 says the agreement also covers the powerful local governments in the key provinces of Gauteng and KwaZulu-Natal.
Steenhuisen said: "Through the votes, the country has made it clear that they do not want one party to dominate our society."
Although it is being billed as a Government of National Unity, the ANC failed to get the third- and fourth-biggest parties - former President Jacob Zuma's uMkhonto weSizwe (MK) party and Julius Malema's Economic Freedom Fighters (EFF) - to join.
The coalition deal is likely to be welcomed by the business sector, who think it will guarantee economic stability and avoid capital flight as it does not include the EFF and MK, who advocate nationalisation of land, mines and banks.
MK had set the resignation of Ramaphosa as one of the conditions for entering into a coalition, which the ANC rejected.
Malema on Thursday evening said the EFF had refused to join a government which included the DA, saying it was part of the "imperialist agenda".
The ANC's deal with the DA and IFP is seen as an attempt to promote racial and ethnic reconciliation following a bruising election campaign.
President Ramaphosa has previously accused the DA - which draws its support mainly from racial minorities - of being "treasonous" and "reactionary".
The ANC was deeply divided over a deal with the DA, with some of it senior leaders - backed up by their allies in the trade union movement and the South African Communist Party (SACP) - preferring a coalition with the EFF and smaller parties or MK.
But Ramaphosa was said to prefer a coalition with the DA and IFP, seeing them as the most reliable partners to tackle South Africa's economic crisis and deteriorating infrastructure.
One of the big obstacles will be the DA's fierce opposition to the ANC's efforts to create a welfare state - especially a government-funded national health service, which the DA rejects, saying it is too expensive and threatens the future of the private health sector.
The DA is also vehemently opposed to the ANC's black economic empowerment policies, seeing them as discriminating against racial minorities while simply leading to the enrichment of the ANC's business cronies - which the ANC denies.
While this deal is a significant step, Steenhuisen acknowledged that South Africa's problems, such as crime and economic issues, would not be "solved overnight" and that the "road ahead will be difficult".
This story was first published by the BBC.