My impression is that they were technically under the Sultan but de-facto ran themselves. How exactly did it work?
I'll try to answer your question, sorry if it doesn't satisfy you. I'll primarily focus on Algeria, though similar things happened in Libya and Tunis.
When the North African provinces were given to the Ottomans by Hayreddin Barbarossa, it was initially ruled directly by the Porte, whose governors are members of corsair households who usually also served as the kapudan pasha (admiral of the Ottoman Navy). This was done because at the time, North Africa was an important naval base for the Ottomans who often wage wars with other Mediterranean powers like Spain. However, as conflict in the Mediterranean dwindles down the north African provinces declines in importance. With that, the Porte started 'transforming' the province from one that tooled for war to economic ones. The province of North Africa, which was at first grouped under the name Ottoman Algeria (Cezayir-i Garp) divided into three, Algeria (Cezayir), Tunisia (Tunus), and Libya (Trablusgarp). The position of the governor of Algeria was separated from the kapudan pasha, and the corsair beylerbeyis were replaced by career men from the center whose focus was to govern the province for financial investments, with these actions the Ottoman rule was changed from a direct to indirect one. These new kind of governors were sent from Istanbul for a term of three years and having bought their post of governor of Algeria, they had no intimate knowledge of the region, relations with members of the Ottoman Algerian elite, or knowledge about the sea. This made them rather unpopular with the Algerian elite, especially considering the previous beylerbeyis was familiar with the Algerian elites because of their corsair origins and ties and usually has real power over the military and navy. With that, Algerian elites and the military (particularly the Janissaries) often rejected or clashed with the governors sent by the Porte. In the early 17th century most of the governors doesn't actually had real power and often imprisoned, with the effective leader of the province being the diwan (council), which were often headed by taifa, head of the corsairs. Starting in 1645, the Porte restored control and the new governor Ahmad Pasha assumed power after supposedly assassinating the taifa Ali Bicnin. However, these governors' influence and effective power had largely been eclipsed by the diwan. But things will change with the incident of 1659
In 1659 the governor of Algeria, Ibrahim Pasha, took a large sum of money that was destined for the corsairs and sent it to Istanbul. This caused discontent among the janissaries and the corsairs, Ibrahim Pasha was jailed, and his successor, already on his way from Istanbul wisely cancelled his trip and went to Izmir instead. The diwan then decided to depose the pasha and transfer the authority to one of themselves, then titled agha. This kind of thing has happened before, and while the usual course of action taken by the Grand Vizier were to restore the law and order by sending an army and executing rebels, in 1659 it was different. Grand Vizier Koprulu Mehmed Pasha sent a letter basically saying that the Algerians can do whatever they want and how insignificant Algeria is. The diwan realized the bad situation they're in tried to reconcile, and while it eventually happened the relations between Algeria and the Porte has been seriously damaged. From 1661 onwards, the governors sent by the Porte were more or less is just for legitimacy and the real power still reside in the hands of the diwan and aghas. The governors were still employed to administrate the region and paid handsomely, but they lost all responsibility for tax collection and for paying the troops, as well as the actual command of the army. Later, the governors were no longer sent from Istanbul but appointed by the diwan and only approved by the Sultan instead.
As you can see by the 17th century Algeria and other North African provinces had been transformed into a "vassal principality". They're still more or less loyal to the Ottomans and provided men and ships when needed, but they often refuse Ottoman orders, especially concerning piracy. In the 17th and 16th century, the North African provinces often refused to adhere to peace treaties with European rulers entered upon by the sultan and only considered such agreements valid if they themselves had negotiated them. While the authorities in Istanbul never entirely accepted this situation, and every now and then commanded their North African subjects to adhere to the agreements that the sultan had concluded, European powers typically signed the separate treaties demanded by the governing bodies of Algiers, Tunis and Tripoli for the safety of their shipping. They even occasionally had fights between themselves, for example in 1705 Algerian soldiers attacked an important commercial market in Tunis.
By the 18th century, the power of the diwan and their aghas were usurped by local janissary strongmen who went to establish their own hereditary rule on the provinces. For example, in 1711 Sokeli Ali Bey eliminated all his Janissary rivals and usurped the title of dey, essentially the governor. Although he still swore loyalty to the Ottoman sultan, he and his descendants ruled Algeria more or less independently until the French conquest in 1830. Similar thing happened in Tunis with Huseyin Alioglu who established the Husaynid dynasty and in Tripoli with Karamanli Ahmed Bey who established the Karamanli dynasty
Sources:
The Ottoman World by Christine Woodhead
The Ottoman Empire and The World Around it by Suraiya Faroqhi