Only Mario knows why he did it.
Having watched it again, maybe there was a whistle. However the reaction of Dzeko (who was stood next to him) and the other players nearby doesn't tie in with the whistle theory. Otherwise why the puzzled expressions of "why the fuck did you just do that" from his team mates?
If he did think he was offside, why did he kick the ball away anyway? I've seen players booked and sent off (2nd yellows) for less in Europe.
Great talent, but he's got to start screwing the nut.