Ganna, victorious at Imola in the recent world championships and winner of the Giro's opening time trial, gave his Ineos-Grenadiers team something to cheer about on the 225km trek from Mileto when he rode away from a group of three breakaway riders in the category one climb to Montescuro.
The 24-year-old, whose previous six wins all came in time trials, crossed the line 34 seconds ahead of a group of overall contenders.
His team mate and pre-race favourite Geraint Thomas, who was forced to abandon after a crash early on the third stage, came to congratulate him afterwards.
"Last night, I messaged with Geraint and he said I should try to go in the breakaway and win the stage, so I did it a little bit for him," Ganna said.
"It was not easy in the climb but when I attacked I just did my own time trial."
Austrian Patrick Konrad came second, and Portugal's Joao Almeida took third place to retain the overall leader's Maglia Rosa.
In the shake-up for the minor places, Hamilton, the Mitchelton-Scott rider, came home fifth, just a place ahead of Team Sunweb's Jai Hindley. Hamilton's teammate Jack Haig was 17th.
The 24-year-old Hindley, from Perth, remains the leading Australian at the Giro in 11th overall, 1 minute 33 seconds behind Almeida.
Hamilton is now 17th, 2:47 down.