First try was using gnus variable gnus-posting-styles which description I have found in Gnus manual. I played a bit with it but I could not get it switch identity according mail headers. So I've tried to find example of it's usage with google. Instead of example I found this elisp file: gnus-pers.el. It allows to define any number of identities and switch them any time. Of course identity can be automatically chosen according mail headers. Cool!
But google helped me one more time. Guy who wrote that elisp file uses xemacs so his code uses some xemacs specific functions (like replace-in-string). But thanks to google I've found another elisp file which implements missing functions for GNU emacs (which I use): functions.el.