By Heidi Vogt in Abuja and Patrick McGroarty in Yola, Nigeria 

ABUJA, Nigeria--Voters packed polling stations across Nigeria on Saturday morning to choose a leader of a country where past election disputes have set off waves of deadly violence.

The outcome of Saturday's contest, and the way Nigerians react, will help set the trajectory of Africa's beleaguered top economy.

The vote appeared to be going off peacefully in the majority of the country, but Boko Haram militants attacked a polling station in the northeastern state of Gombe, killing three police officers and four voters, according to residents.

President Goodluck Jonathan--in power since 2010--is running against Muhammadu Buhari, a fourth-time candidate with wide support among Muslims in the north of the country. Mr. Jonathan's support base is the predominantly Christian south.

An incumbent has never lost an election in Nigeria, but polls have shown the two in a tight race. When Mr. Jonathan beat Mr. Buhari in 2011, riots broke out across northern Nigeria, where Muslims and Christians mingle in cities such as Kaduna and Kano. Human Rights Watch says more than 800 were killed.

But the two men have pledged to make things different this time. Mr. Jonathan and Mr. Buhari embraced each other in the capital on Thursday and signed a peace pact that committed both to encouraging a free, fair and orderly vote.

Technical snafus bedeviled early voting registration. Nigeria is using an electronic verification system for the first time and the hand-held machines to read cards and fingerprints were experiencing glitches.

Many had trouble reading the biometric data on voter's cards, causing delays. Local media reported that even President Jonathan was having trouble--three different card readers failed to identify Mr. Jonathan as he tried to register in his home state of Bayelsa.

Monitors said similar reports were coming in from across the country.

"It's certainly something we see not just in a few places. It's not limited to one, two or three polling stations," said Eberhard Laue, a spokesman for the European Union monitoring team.

The government went to extraordinary lengths to assure Nigerians that they would be safe on Saturday, closing land and sea borders for days and barring even most road traffic. Officials said a polling station was within walking distance for most of the 50 million voters.

The election has also been complicated by the threat of Boko Haram militants. It was delayed for six weeks to give the military more time to beat back the insurgents, who vowed to disrupt the vote.

In Gombe on Saturday, the militants opened fire as they drove into village polling stations in Nafada and Dukku administrative areas, according to residents who escaped.

"We saw the dead bodies of three policeman and a voter on the ground," said Ibrahim Hussaini, who lives in Biri Bolawa village. A resident of another nearby village--a man named Saleh Musa--said the attackers then came to his polling station and shot three voters dead.

Nigerian police spokesman Emmanuel Ojukwu confirmed that there was an attack, but said he didn't have details.

The military has retaken a large amount of the northeast from the militants, but hundreds of thousands are displaced and it is unclear how many will actually go out to the polls in the battered region.

Voters turned out to vote in large numbers in Maiduguri, the capital city of Borno state, which has been at the center of the war against Boko Haram. But in another northeast state, Adamawa, turnout was more sparse.

Officials set up polling stations for two of the state's 21 districts in the state capital of Yola because the areas remain deeply unsafe. At many polling stations for voters from those districts, less than 20% of registered voters had managed to make it to the voting booth.

"We are blaming our incumbent government," said Habu Umaru, one of about 80 people out of 650 eligible voters in his district who made it to the polls. "Our people have been under attack and now you want them to travel here to exercise their civil liberty. They don't have the means."

A year ago, Nigeria passed South Africa as the biggest economy in Africa. Since then oil prices have plunged to half their former value. The price drop has drained revenue from Africa's top crude producer, hammered Nigeria's currency and dimmed its growth outlook.

Mr. Jonathan, a 57-year-old former zoology professor, says he will raise new revenue from a luxury tax and cut spending. Mr. Buhari, 72, who briefly ruled Nigeria as a military dictator in the 1980s, has vowed to tackle corruption.

The tenor of the vote varied widely by location. In the capital city of Abuja, residents openly discussed voting for rival candidates. Building contractor Tony Julius said he had decided to vote for Mr. Buhari but his wife was sticking with Mr. Jonathan, who they both supported in 2011. Mr. Julius said he was tired of constant power cuts and he didn't think Mr. Jonathan had done enough to improve security in the north. Blessing Julius, his wife, said she wanted to give Jonathan more time. They laughed at the dispute and he joked about shutting her out of the house if his man loses.

In Gombi, a town near Nigeria's northeastern border with Cameroon that Boko Haram attacked in November and January, almost everyone was voting for Mr. Buhari. Thousands of people waited in a grade school courtyard to cast their ballots.

Bahiru Bamanga waited two hours to have his identity verified. At the front of a long line under a wizened baobab tree, an election official scanned his voting card and his left thumb.

"I need peace. I don't want to suffer always," said the 22-year-old welder. In the town, blackened steeples of three destroyed churches loom over a row of shops destroyed during Boko Haram's November assault.

Uwmi Garaba fled with her husband and four children into the vast desolate plains outside of town during each Boko Haram assault. "We expect Buhari to make it safe again in Gombi," said the 22-year-old mother of two.

Write to Heidi Vogt at heidi.vogt@wsj.com and Patrick McGroarty at patrick.mcgroarty@wsj.com