Good advice, though I prefer "Why am I being detained?" as the initial question. This is a stronger question in my opinion. If, as a reasonable person, you believe you are not free to go, then you are being detained. You should not have to ask if you are or not, you should know already.
The question of why will possibly confuse the public servant into the response of "you are not being detained", at which point you may simply walk away. If that is not the response, then at least you will know you are being formally detained and will hopefully be told the reason for the detention. This question effectively kills two birds with one stone.
I think most encounters (that occur while you are on foot) will start with a question like, "can I talk to you for a minute?" In my opinion, these should simply be answered with a firm "NO", and continue on your way, unless you really want to talk to the person you don't know for some reason. If it comes in the form of a command rather than a question, then I would personally assume I was being detained and start with the "why" question.
The more polite among you may wish the officer a good day, but I harbor no good feelings whatsoever toward our modern militarized police forces. I think our current police forces in many cities are in fact unconstitutional standing armies put in place counter to the Posse Comitatus Act. Many police forces receive federal funding, federal training, have weaponry that is on par with our military forces, and will follow federal orders if given. It is tantamount to a federal military occupation of our cities. Sorry for the rant.