President Chen Shui-bian's Democratic Progressive Party eked out the narrowest victory in Taiwanese political history in crucial mayoral elections here on Saturday, but the opposition Nationalist Party quickly said it would seek a recount.

The Central Election Commission in Taipei announced that with all ballots counted, the Democratic Progressive Party candidate in Kaohsiung had won by 1,120 votes out of more than 760,000 cast, a victory margin of about one seventh of a percent. On an island with already deep political divisions, the paper-thin margin and the possibility of a court fight are likely to cause further political friction in the weeks ahead.

Joanna Lei, a senior Nationalist lawmaker, said that party officials would ask the municipal election commission here to conduct a recount. Municipal election commissions have the discretion to order recounts when the outcome is decided by less than 1 percent of the vote.

Taiwan's legislature discussed whether to require recounts in close elections after President Chen narrowly won re-election in 2004, but never acted on the idea.

Both parties also threatened to sue each other in district court here on Monday, and began demanding that all ballot papers and other records be sealed at the courthouse.

Democratic Progressive Party officials accused the Nationalists on Friday and Saturday of paying people to attend a rally on Friday night; the Nationalists accused the Democratic Progressive Party of violating a law that bars parties from making any statements that may influence the election outcome after 10 p.m. the night before the polls open.

Even before the polls closed, the authorities here prepared for a close election by assembling a dozen busloads of riot police and heavy steel barriers fringed with barbed wire to defend the courthouse, where ballots are sent after an initial count in polling districts. Kaohsiung has a history of violent street protests.

But local Nationalists appeared more stunned than angry, and the streets around the courthouse were quiet on Saturday evening. Police officers at the courthouse sat on the pavement next to their riot shields or snoozed on the nearby buses.

The Nationalist Party had sought to turn mayoral elections here and in Taipei on Saturday into an informal referendum on President Chen, who has been dogged by allegations that he and his family have committed financial impropriety.

A Nationalist Party candidate, Hau Lung-bin, won the election in Taipei by a wide margin. That had been expected because northern Taiwan, including Taipei, tends to support Nationalist candidates while southern Taiwan, including Kaohsiung, more often votes for Democratic Progressive Party candidates.

While Saturday's vote represented a reprieve of sorts for President Chen, it may have also strengthened the Nationalist Party, which favors closer relations with mainland China. James Soong, a former Nationalist who split with the party and set up a rival party, the People First Party, ran as an independent in the Taipei mayoral race but attracted just 4 percent of the vote.

Mr. Soong announced after the count that he would leave politics, which could clear the way for the People First Party to combine with the Nationalists, a move the two parties have been negotiating off and on for at least three years.

The vote count here showed Chen Chu, the Democratic Progressive Party candidate, with 379,417 votes, compared to 378,297 for Huang Chun-ying, the Nationalist candidate.