The Andalusians rode their luck at times, but took all three points thanks to a goal in each half from Duda and Salomon Rondon.
And although they remain bottom of the table with 10 rounds of the championship remaining, Manuel Pellegrini's side are now level on points with both Almeria and Hercules, and just two adrift of 17th-placed Sporting Gijon.
Their win looked unlikely early on with Sociedad dominant in the opening minutes.
Xabi Prieto could have given the home side the lead after just six minutes, but the midfielder shot straight at Willy Caballero with only the Malaga keeper to beat.
Malaga then took the lead after 24 minutes following a shocking mistake by Sociedad keeper Claudio Bravo.
The Chilean failed to make contact with Duda's cross and could only watch in horror as the Portugal midfielder's centre drifted into the back of the net.
Raul Tamudo missed a great chance to equalise just before the interval and Caballero made an important stop to deny Diego Rivas 10 minutes after the restart.
But against the run of play, Malaga went two up a minute later, when Rondon sent a powerful header past Bravo from Eliseu Pereira's pinpoint cross.
The result leaves Sociedad in 10th and Malaga in 20th, with nine points between the two sides, but the Basques appear safe already.