In 1984, under pressure from the U.S. and the Contras, the Sandinistas agreed to hold elections. This first election was won by Daniel Ortega Saavedra, a member of the FSLN, causing fighting with the Contras to continue.
The assumption of power by the FSLN, which quickly became more openly socialist and abusive towards human rights, led to the creation of a new rebel group called the Contras. When Reagan beat out Carter in the 1980 U.S. presidential elections, he made it a priority of his to support the Contras. Reagan got shut down by Congress in 1982, however, when they passed the Boland Amendment, which prohibited the federal government from providing aid to the Contras for the purpose of taking down the Sandinistas as that was seen as unjustifiably assisting in the overthrow of a legitimate government. This didn't stop Reagon from advocating for support for the Contras for other reasons.
In 1984, under pressure from the U.S. and the Contras, the Sandinistas agreed to hold elections. This first election was won by Daniel Ortega Saavedra, a member of the FSLN, causing fighting with the Contras to continue.

Ortega Being Sworn in as President
MacNeil/Lehrer News Hour

Key Members of the Iran-Contra Affair
MacNeil/Lehrer News Hour
The 1986 Iran-Contra Affair revealed that despite the Boland Amendment, several dozen Reagan officials were involved in covertly funneling money to the Contras. They did so by selling weapons to Iran, which was subject to a U.S. embargo, in exchange for the release of U.S. hostages held by Hezbollah, and in the process sent some of the profits to the Contras.
The terrible state of Nicaragua and thousands of lives lost fighting the Contras made the Sandinista government unpopular by the late 1980s. The FSLN leaders thus decided that they had to be more flexible, culminating in them signing the Central American Peace Accords in August of 1987. These accords aimed for democratization and regional security. Following these accords, Ortega agreed to internationally supervised elections in 1990, believing that he would win again, but he ended up losing to Violeta Barrios de Chamorro, who represented a coalition of opposing parties. With the election of Chamorro, the Contras disbanded. Ortega was beaten out repeatedly in elections until 2006, and since then he has maintained his position as the president of Nicaragua.