What is the correct way of praying on behalf of others when doing ziyara of a shrine? Do I make the intention to pray 2 rakaat then gift the reward to someone?

Yes it’s as you said