Katarina Bulatovic was the star performer for Montenegro, ending the game with nine goals.
In the first half only Montenegro were ever more than one goal ahead, at 6-4, but there was no separating the sides at the break as they went in 13-13, Andela Bulatovic equalising just before the whistle from a swift counter.
The scores remained close for most of the second period, but it was a five-minute spell between 45 and 50 minutes that effectively decided the contest.
With the scores at 19-19 Montenegro scored five without response, forcing Spain to try to overturn the deficit in the final 10 minutes.
By going forward, Spain were vulnerable on the break but they almost pulled level late on with the game ending 27-26.
Montenegro will face Norway in Saturday's final.