It goes back to the Treaty of Karlowitz in 1699, concluding the Austro-Ottoman War of 1863-97 (also called the Great Turkish War or the War of the Holy League). This marked the end of the Turkish expansion in Central Europe.
At the time, much of the East Coast of Adriatic Sea belong to the Republic of Venice. They ruled over Istria, practically all of the islands in the Adriatic, Dalmatian coast and Bay of Kotor. A notable exception was the Republic of Ragusa (nowadays Dubrovnik) which was a vassal of the Ottoman Empire. Due to hostile treatment by the Venetians, the Republic of Ragusa insisted that its territory be separated from Venetian possessions in order to avoid raids by Venice which had been commonplace before. According to the aforementioned treaty, two narrow corridors were established to the north (Neum) and south (Sutorina) of the Republic of Ragusa. They were given to the Ottomans who thus gained access to the sea in two places, cutting Venetian territory from Ragusa.
Venetian and Ragusan territories were eventually integrated into Austria, but the two corridors remained Ottoman until the occupation of Bosnia and Herzegovina by Austria-Hungary in 1878. It was de facto under its control ever since, but only formally annexed in 1908 (provoking the Bosnian crisis and ultimately WWI).
After WWI, the newly created Yugoslavia was divided into new administrative divisions which had little to do with history, since the aim of the country was to unite the population and neglect the historical divisions. The municipalities, however, followed the old borders so there was a kind of continuity, even if "Bosnia and Herzegovina" didn't exist as such, legally.
After WWII, the old borders were revived and Neum became part of the Socialist Republic of Bosnia and Herzegovina, thus renewing the situation as it was before WWI. Sutorina, on the other hand, was informally ceded to the Socialist Republic of Montenegro. After the breakup of Yugoslavia, Bosnia and Herzegovina actually claimed sovereignty over Sutorina until 2015, when they finally signed a border agreement with Montenegro relinquishing these claims.
Just why Socialist Yugoslavia gave Neum back to Bosnia and Herzegovina is not really known. It may be seen as an attempt to "undo" what the Kingdom Yugoslavia did to the administrative divisions, or an attempt to ensure Bosnia and Herzegovina access to the sea. Anyway, it was little more than a dead letter on paper, since Croatia, Bosnia and Herzegovina, and Montenegro were all part of Yugoslavia and these borders were not seen as anything of consequence.
I.
The creation of Neum strip dates back to Treaty of Karlowitz in 1699, following the great anti-Ottoman coalition war of 1684-1699. In the Dalmatian front of the war Venetian forces took large swaths of Ottoman held Dalmatia (in today's Croatia) and Bay of Kotor (in today's Montenegro) in the process surrounding the Dubrovnik /Ragusa Republic, a nominally Ottoman vassal, but with relatively high degree of autonomy and independance. While in many ways Ragusans cheered the Christian victories, sharing a land border with Venice created a possibility of Venetian attack on the Ragusan Republic. Ragusan's decided the best way to avoid this hypothetical attack was to cede small strips of their territories to Ottomans so that there is no direct land border between Ragusan and Venetian territories, and any Venetian attack would have to cross Ottoman territory and provoke a larger war. This lead to creation of Neum strip in the north and Sutorina strip in the south in the Treaty of Karlowith. This two strips were confirmed in the conclusions of following wars in treaties of Passarowritz (1719) and Belgrade (1739) and have pretty much formed the border for next two centuries.
Napoleon's conquest ended both the Venetian and Ragusan Republics, and while I am not sure if the strips existed in the Illyrian provinces, they were reconfirmed as Ottoman possessions following Napoleon's downfall, while both Venetian and Ragusan territories went to Austria. The borders with the strips were reconfirmed in 1878 Berlin Congress as part of Bosnia territories which Austro-Hungarian Empire first occupied and administered and then annexed in 1908, with the strips were formative parts of the province.
II.
With the end of WWI, the area was integrated into Kingdom of SHS / Jugoslavija. The new state had several changes of it's internal divisons, and the province borders as they existed pre-1918 were in most part redrawn, and the strips disappeared and were included in their border regions..
During WW2, the communist partisan AVNOJ (Anti-Fascist Council for the National Liberation of Yugoslavia Antifašističko vijeće narodnog oslobođenja Jugoslavije – AVNOJ ) the sort of political goverment of the anti-fascist movement decided that the future borders of the Bosnia and Herzegovina should be made based on it's borders of Berlin Congress of 1878, the reasoning being mostly to avoid ethnic redrawing of maps and the chaos it would cause - which wouldn't fit with the communist "Brotherhood and Unity" ideology which wanted to dispel the ethnic-nationalist divisions.
And so indeed following the end of WW2, and establishment of the SFRY (Socialist Federal Republic of Yugoslavia) Bosnia and Herzegovina republic was formed based on the old borders which included both strips, the Neum one, and the Sutorina one, which divided Socialist Republic of Croatia from Socialist Republic of Montenegro. Interestingly, despite Neum strip remaining, the Sutorina strip itself was in 1947-1952 exchanged to Montenegro completely, removing this BiH southern exit to the sea, and creating Croatian-Montenegrin land border.
During the dissolution of Yugoslavia, one of the main insistences of the internal community was keeping of the pre-1991 borders of the republics, which cemented Neum remaining in the Bosnia & Herzegovina.