How is the LOCAL dialplan function susposed to work?

The wiki[1] has a description of what it is for. It’s a locally scoped variable that after the Gosub returns it is returned to its previous state. Underneath it is doing a normal Set() but maintains state that it cleans up when Gosub returns so it return to the original state.

[1] https://wiki.asterisk.org/wiki/display/AST/Function_LOCAL