An earthquake flattened farming villages near the Great Wall on Saturday, killing 47 people and leaving at least 20,000 homeless in the bitter cold of the northern mountains.

The magnitude-6.2 quake injured some 2,000 people, more than 250 of them seriously, the official Xinhua news agency reported.Mud and brick houses toppled across two counties in the Yan Mountains. Buildings shook in Beijing, 150 miles south of the epicenter.

Residents huddled under makeshift shelters of twigs and canvas, and a goat stood amid a 6-foot-high pile of rubble in the first television footage of the scene.

With overnight temperatures expected to drop to minus 7 degrees, local officials scrambled to help the homeless.

Zhangbei county authorities rushed 30,000 blankets, 100 mattresses and padded overcoats to the scene, state-run television reported. Troops garrisoned in nearby Zhangjiakou sent five medical teams.

Aftershocks - 123 in the first hours after the quake - coursed through the area, with the strongest being magnitude-4.6, Xinhua said.

Rescue teams found the county seat of Zhangbei devastated, with most buildings destroyed and 20,000 families without homes. Another 800 houses had collapsed or cracked in the neighboring county seat of Shangyi, on Hebei province's border with Inner Mongolia.

Eighty percent of homes were flattened in four towns along the two counties' border, said Huangfu Qing, a seismologist coordinating rescue work.

Both counties border one of a patchwork of fortifications running along mountain ridges that form the Great Wall. It was unclear whether any of the 1,860-mile wall, begun in the third century B.C., was damaged.

The quake struck at 11:50 a.m., when many people were indoors preparing lunch.

"Houses split, walls cracked and glass shattered," said Wang Haiyan of the seismology office in Shangyi. She said the region's rough terrain hampered assessment of casualties and damage.

People in Beijing felt apartment buildings quiver. And visitors at the Zhangjiakou Guest House in Zhangjiakou, 60 miles from Shangyi, ran outside in fright.

"The building shook, things shook" for several seconds, said a switchboard operator at the hotel who only gave her surname, Zhao.

Zhangjiakou was for centuries an important trading town linking Beijing with Mongolia.

President Jiang Zemin and Premier Li Peng called Hebei authorities to make sure rescue work took top priority, state TV reported.