Mladen Krstajic's team entered the match knowing that a victory would see them advance to the round of 16, but Granit Xhaka's second-half effort cancelled an early header from Aleksandar Mitrovic, before Xherdan Shaqiri won it for Switzerland in the final minute.
Switzerland have joined Brazil on four points, while Serbia sit third on three points ahead of the final matchday, which sees Switzerland take on already-eliminated Costa Rica, and Serbia lock horns with Brazil.
Xhaka had the first shot of the match inside four minutes, but the Switzerland midfielder's effort was always rising over the Serbian crossbar.
It was Serbia that made the breakthrough in the fifth minute, however, when Mitrovic headed a wonderful Dusan Tadic cross into the far corner of the net.
Mitrovic had brought a fine save from Yann Sommer moments before, but the Switzerland goalkeeper could not keep out the Newcastle United forward's next effort as the deadlock was broken.
Mitrovic then headed over the Switzerland crossbar in the 14th minute before Sergej Milinkovic-Savic missed the target from distance as opportunities continued to arrive in Kaliningrad.
Tadic struck into the arms of Sommer down the other end of the field as Serbia had a flurry of chances late in the first half - also coming close through Dusko Tosic and Nemanja Matic from corners - but the two teams headed down the tunnel separated by Mitrovic's fifth-minute header.
Rodriguez found space outside the Serbia box early in the second period before striking towards goal, but the left-back's effort was always moving wide of the post.
Switzerland were level in the 52nd minute, however, when Xhaka's long-range strike flew past Stojkovic before nestling in the back of the Serbia net.
Shaqiri almost sent Switzerland ahead just before the hour when he curled a spectacular effort towards the top corner, but the ball hit the outside of the post before dropping behind.
Serbia had a glorious chance to regain the lead 20 minutes from time when Aleksandar Kolarov delivered a sensational low cross into the Switzerland box, but Mitrovic just could not make contact at the far post.
That opportunity came after Mitrovic appeared to be wrestled to the ground by two defenders inside the Switzerland box, but neither the referee nor VAR were interested.
The two teams were still locked at 1-1 entering the final 10 minutes of action, but it was Switzerland pushing for the second, with Shaqiri flashing another attempt over the crossbar before Mario Gavranovic was denied by Stojkovic.
Switzerland won it in the final minute, however, when Shaqiri raced clear of the Serbian defence before expertly slotting into the bottom corner.
Shaqiri almost had his second in the closing moments, but the Stoke City attacker fired wide of the post as the match finished 2-1 to Switzerland.
SERBIA (4-2-3-1): Stojkovic; Ivanovic, Milenkovic, Tosic, Kolarov; Matic, Milivojevic (Radonjic 81'); Tadic, Milinkovic-Savic, Kostic (Ljajic 64'); Mitrovic
SWITZERLAND (4-3-3): Sommer; Lichtsteiner, Schar, Akanji, Rodriguez; Behrami, Xhaka; Shaqiri, Dzemaili (Embolo 73'), Zuber (Drmic 93'); Seferovic (Gavranovic 46')