China ultimately had more allure than Kazakhstan.
With the award, Beijing will become the first city to have hosted the summer and winter Olympics.
In the prelude to the announcement in Kuala Lumpur, the Chinese president Xi Jinping promised a spectacular show.
In a video message that was relayed to the special IOC session, Xi said that a Beijing games would inspire more than 1.3 billion Chinese to take up winter sports.
"The Chinese people will present to the world a fantastic, extraordinary and excellent Olympic winter games," Xi said.
Almaty's main marketing plank was its offer of "real" snow and shorter travelling times between sports venues for athletes.
But China's experience of holding the 2008 summer Olympics is thought to have swayed the 85 members of the IOC who decided on the venue in a secret vote during a special session on Friday.
Performers and uniformed volunteers erupted into dancing and flag-waving in Beijing after the announcement.
Zhang Hong, the 1,000m speed skating gold medal winner at the 2014 Sochi Games, told CCTV: "I am so excited. This is China's pride."
Young volunteers wearing matching polo shirts waved the Chinese national flag and bid logos outside the Bird's Nest stadium, where the 2022 opening ceremony will be held.
There were similar explosions of joy in Zhangjiakou, where many of the skiing events will be held in 2022.