At a ceremony in Nairobi, the two men put their signatures to a power-sharing deal brokered by ex-UN head Kofi Annan.
A coalition government comprising members of the current ruling party and opposition will now be formed.
Some 1,500 people died in political violence after Mr Odinga said he was robbed of victory in December’s polls.
International observers agreed the count was flawed.
Violence has mostly receded, but tensions are still running extremely high.
Negotiations between the government and Mr Odinga’s Orange Democratic Movement (ODM) lasted more than a month, stalling several times.
Discussions centred on the creation of the post of prime minister, which will be taken by Mr Odinga.
Speaking after the deal was signed, Mr Annan said the division of posts in the new government – including the new position of prime minister and two deputy prime ministers – would reflect the political parties’ strengths in parliament.
Under the agreement, the new prime minister will have “authority to co-ordinate and supervise the execution of government functions”.
Mr Annan urged all Kenyans to support the agreement, saying: “Compromise was necessary for the survival of this country.”
Mr Kibaki said: “My government will fully support implementation of the agreement reached… until we achieve the result that we all want.”
Mr Odinga said: “With the signing of this agreement, we have opened a new chapter in our country’s history… we on our side are completely committed [to] this agreement.”
Both men thanked those who had stood by Kenya in what Mr Odinga called its “hour of need”, including Mr Annan, the African Union, the European Union, the United States and the UN.
They also urged Kenyans to move forward together without ethnic divisions.
The post-election violence saw thousands of people targeted because they belonged to ethnic groups seen as either pro-government or pro-opposition.
About 600,000 people have fled their homes and some have been forced back to their ancestral homelands.