In 1510, it was taken by Don Pedro Navarro, Count of Oliveto for Spain, and, in 1523, it was assigned to the Knights of St. John, who had lately been expelled by the Ottoman Turks from their stronghold on the island of Rhodes. Finding themselves in very hostile territory, the Knights enhanced the city's walls and other defenses. Though built on top of a number of older buildings (possibly including a Roman public bath), much of the earliest defensive structures of the Tripoli castle (or "Assaraya al-Hamra", i.e., the "Red Castle") are attributed to the Knights of St John.
Having previously combated piracy from their base on Rhodes, the reason that the Knights were given charge of the city was to prevent it from relapsing into the nest of Barbary pirates as it had been prior to the Spanish occupation. The disruption the pirates caused to the Christian shipping lanes in the Mediterranean had been one of the main incentives for the Spanish conquest of the city.
The knights kept the city with some trouble until 1551, when they were compelled to surrender to the Ottomans, led by Muslim Turk Turgut Reis. Turgut Reis served as pasha of Tripoli, during his rule he adorned and built up the city, making it one of the most impressive cities along the North African Coast. Turgut was also buried in Tripoli after his death in 1565. His body was taken from Malta, where he had fallen during the Ottoman siege of the island, to a tomb in the mosque he had established close to his palace in Tripoli. The palace has since disappeared (supposedly it was situated between the so called "Ottoman prison" and the arch of Marcus Aurelius), but the mosque, along with his tomb, still stands, close to the Bab Al-Bahr gate.
After the capture by the Ottoman Turks, Tripoli once again became a base of operation for Barbary pirates. One of several Western attempts to dislodge them again was a Royal Navy attack under John Narborough in 1675, of which a vivid eye-witness account has survived. Effective Ottoman rule during this period (1551–1711) was often hampered by the local Janissary corps. Intended to function as enforcers of local administration, the captain of the Janissaries and his cronies were often the de facto rulers.
In 1711, Ahmed Karamanli, a Janissary officer of Turkish origin, killed the Ottoman governor, the "Pasha", and established himself as ruler of the Tripolitania region. By 1714, he had asserted a sort of semi-independence from the Ottoman Sultan, heralding in the Karamanli dynasty. The Pashas of Tripoli were expected to pay a regular tributary tax to the Sultan, but were in all other aspects rulers of an independent kingdom. This order of things continued under the rule of his descendants, accompanied by the brazen piracy and blackmailing until 1835, when the Ottoman Empire took advantage of an internal struggle and re-established its authority.
The Ottoman province (vilayet) of Tripoli (including the dependent sanjak of Cyrenaica) lay along the southern shore of the Mediterranean between Tunisia in the west and Egypt in the east. Besides the city itself, the area included Cyrenaica (the Barca plateau), the chain of oases in the Aujila depression, Fezzan and the oases of Ghadames and Ghat, separated by sandy and stony wastelands.