Morocco sealed its place by winning in Ivory Coast 2-0 in what was a straight shootout for the place from Group C.Tunisia secured the draw it needed, 0-0 at home against Libya, to qualify from Group A and edge out Congo.
But after weathering an early storm, including a potential penalty call, Morocco scored two goals in the space of five minutes to put themselves on their way to the finals, in which they last appeared in 1998, in France.
Morocco beat Ivory Coast, away by 2 goals to nil while Tunisia held Libya at home to a goalless draw.
Libya had been forced to play all of their home games at neutral venues, including Tunisia, because of a Federation Internationale de Football Association ban on internationals in the country, where the security situation is judged unsafe for visiting teams.
Tunisia's point ensures their fifth World Cup participation - and their first since 2006 - but ensures that the Democratic Republic of Congo's wait to return to the global showpiece for the first time since 1974 will continue.
Morocco's victory over the West African giants means three countries from North Africa made it to Russian Federation with Egypt and Tunisia having qualified already.
Tempers spilled over as the two sides exchanged wild tackles and punches in the closing stages and Ivorian fans pelted the pitch with objects.
The Atlas Lions then doubled their lead just five minutes later through Captain Mehdi Benatia's neat finish off a corner, ensuring Herve Renard's men finished top of Group C and the only team in the overall qualifiers, which did not concede a single goal.
Ivory Coast, who never looked like recovering from the first-half blows, came second with eight points followed by Gabon with six and Mali with four.