Against a backdrop of militant Islamist attacks, Secretary of State John F. Kerry on Sunday urged Nigeria’s president and his leading opponent in upcoming elections to accept the results of next month’s vote and encourage their supporters to eschew postelection violence.

Kerry flew into Lagos and met first at the Colonial-era State House with President Goodluck Jonathan, then at the U.S. Consulate with Muhammadu Buhari. Jonathan and Buhari are the two front-runners in a field of 14. Kerry also spoke by phone with the chairman of Nigeria’s electoral commission.

It is unusual, though not unprecedented, for a senior U.S. official to visit a country just weeks before an election. In general, U.S. representatives strive to avoid the appearance of trying to influence the outcome, but Kerry’s stop in Lagos reflects the growing concern that the election could trigger waves of violence. In the last, disputed presidential election in 2011, about 800 people died in the resulting riots.

The fear now is that the mayhem could spiral out of control, allowing the militant Islamist group Boko Haram to make further advances. The al-Qaeda-inspired group has rampaged through the countryside of Africa’s most populous nation, using bombings, killings and kidnappings in an attempt to overthrow the government and replace it with an Islamic state. Boko Haram now controls large parts of the country’s northeast, and the military has been impotent at stopping its attacks on schools, marketplaces and villages.

“The fact is that one of the best ways to fight back against Boko Haram and similar groups is by protecting the peaceful, credible and transparent elections that are essential to any thriving democracy, and certainly essential to the largest democracy in Africa,” Kerry said. “It’s imperative that these elections happen on time, on schedule. And that they are an improvement over past elections. They need to set a new standard for this democracy. That means Nigerians have to not only reject violence, but they have to actually promote peace.”

Both candidates already have signed an agreement to encourage their supporters to refrain from election-related violence and to speak out against any violence that happens.

As if to underscore the threat, suspected Boko Haram militants attacked the city of Maiduguri in northeastern Nigeria, hours before Kerry’s arrival. Initial reports put casualties in the dozens. There also was a Boko Haram raid on the nearby town of Monguno, 85 miles north of Maiduguri. Army forces reportedly were overwhelmed by insurgents who set houses on fire.

Kerry has been talking increasingly about the need to combat violent extremists by paying attention to the conditions that fuel their rage. In a speech at the World Economic Forum at Davos, Switzerland, on Friday, he singled out bad governance as one type of kindling for a fire of despair.

Nigerians vote Feb. 14 for a president in a race that is considered the most polarizing and competitive election since military rule ended in 1999.

Jonathan, the incumbent whose People’s Democratic Party has held power ever since, draws his support from the largely Christian, oil-producing south. He assumed office after the previous president died midway through his term, and some Nigerians believe his reelection bid violates his party’s unofficial policy of rotating the presidency between candidates from the Christian south and the Muslim north.

His closest competitor is Buhari, a former general whose stronghold is in the mostly Muslim north. His supporters in the All Progressive Congress party hope he can defeat Boko Haram as Jonathan’s government has failed to do. During a previous stint as head of state in the mid-1980s, Buhari placed restrictions on the press and political freedoms.

Senior State Department officials, who spoke to reporters traveling with Kerry under conditions of anonymity, said the election is likely to be a drawn-out affair. Under Nigeria’s complicated election rules, a candidate must win 50 percent of the vote and at least 25 percent of the vote in two-thirds of the states. If that doesn’t happen, a runoff will be held a week later. And if that still doesn’t decide the race, a second runoff will be held after another week, with the victor being the one who wins a simple majority.

This election is further complicated by the displacement of almost 1 million Nigerians by Boko Haram’s insurgency and the inability of election workers to go into a war zone. The twin factors make it difficult to stage a credible election, making it more likely that the results will be disputed and, in turn, increasing the potential for violence.