I’ve seen maps where the Ottoman Empire expansion stops at present day Algeria. Did the Ottoman and Moroccans fight for territory or was there some sort of an agreement reached?
The Ottomans did not attempt to conquer Morocco, although they did sometimes interfere in Moroccan politics. In 1576 an Ottoman-Algerian force entered Morocco, to back a Moroccan claimant to the throne against the Moroccan king.
Maps show Algeria as Ottoman territory but the Ottomans never conquered or directly ruled it. Rather, the ruler of Algiers sought Ottoman assistance in his war against Spain in the 1510s, and accepted Ottoman suzerainty in return. Algiers, Tunis and Tripoli were all vassal states of the Ottoman Empire. The only part of North Africa that was directly ruled by the Ottomans was Egypt.
The rulers of Algiers, Tunis and Tripoli were pirates (or, to be precise, privateers, since they were doing it legally), and they contributed forces to the Ottoman navy (at least one of the Algiers beys served as the Ottomans' Grand Admiral), as well as generally preying on the shipping of the Ottomans' European enemies. The territories were run by a military class that was quite Ottoman in culture, but they were separate from the main ruling elite of the empire. Whereas in Ottoman provinces, the governors would be appointed by the Sultan from among an empire-wide ruling elite who were circulated through the provinces and senior government posts in the capital, the rulers of Algiers, Tunis and Tripoli would be chosen from among their own military elites. Often they would pretty much sort it out among themselves and then the Sultan would just approve the choice.
From the late 17th century, the North African states increasingly started essentially running their own foreign policies with the European countries, which called them the "Barbary States." European countries were keen to build relations with them, so that their ships wouldn't get attacked by the privateers. So the rulers could essentially charge protection money. By the late 18th century they were pretty much independent in governance - they still recognized the Sultan as their sovereign, and sent annual tribute, but they ran their own affairs. Right through to the end, however, there were cultural links and opportunities for North African statesmen in the Ottoman Empire - the 19th-century reformist Tunisian ruler Khayr al-Din Pasha ended up as Ottoman Grand Vizier. In the case of Tripoli, however, the Ottoman military invaded it in the late 19th century, intervening in a civil war there, and then incorporated it properly into the empire for a few decades, until the Italian occupation in 1911.