If a question is duplicate, it should be outright marked duplicate and not because someone's answer is not accepted. Right?
Correct. Closing a question as a duplicate is not a "punishment", and even if it were, it would not be appropriate to "punish" someone for exercising their right to accept a different answer.
I asked a question on stack overflow which received 2 answers. The first answer didn't work first time, so I put a comment on it for clarification. In the meanwhile, I tried second answer's solution and it worked. I accepted the second answer.
What you did here was perfectly reasonable and correct.
After this, the user who gave first answer deleted his answer and then my question got the duplicate flag, plus a vote to close the question.
Indeed, this looks a little bit sketchy.
My first guess would be that, as he begin to do some additional research after reading your comment, he came across a duplicate question, and therefore decided to delete his answer and vote to close the question as a duplicate. This would actually be perfectly reasonable—if you come across a duplicate, you should flag the new question as being a duplicate and you should not answer duplicate questions.
However, that does not appear to be the case here… Looking at his now-deleted answer, the original revision contained a link to the proposed duplicate at the bottom, in the guise of a "more info" link. So it does look like this was retaliatory behavior on this user's part, and that he was in the wrong here. Your question was a duplicate from the outset, and since he found that duplicate before submitting his answer, he should have flagged it as a duplicate instead of posting an answer.
That said, you have not been hurt by this action, as having your question flagged as a duplicate is not a bad thing. Nothing really needs to be done to "fix" it, other than perhaps a moderator having a friendly conversion with Ramesh. Or perhaps he will see this Meta discussion.
If you disagree that your question is a duplicate of the proposed question, then you should take this opportunity to edit your question and clarify how it is different. Otherwise, you run the risk of 4 other users agreeing with Ramesh's vote and your question getting closed as a duplicate. Of course, if it is a duplicate, then like I said above—there's nothing wrong with having it closed as such. In fact, this is a good thing because it gets you an answer that has been already been vetted by the community.