Who are Brahma, Vishnu and Shiva and where do they come from? What is their relation with lord krishna?
Each brahmand also has its own Brahma, Vishnu and Shiv. Thus, there are uncountable Brahmas, Vishnus and Shivas in the universe. They are representatives of the Divine government here in the material plane. In other words, they have established an embassy or branch office in every brahmand all over the universe. From their place in each brahmand, they perform their duties of creation, protection and destruction.
These uncountable descensions of the Divine power in the form of the Brahma, Vishnu and Shiv in each brahmand are all ansh of Maha Vishnu of the Divine abode (ansh means a part of, or partial manifestation of). Maha Vishnu is the creator of the entire universe. That Maha Vishnu is ansh of Shree Krishna, who resides in Golok abode.
The Bhagwatam says that Bhagwan Shree Krishna has uncountable avatar. Out of all of them, His first is called 'Pratham Purushavatar', which is the form of Maha Vishnu. He takes this form in order to initiate the creation of the universe. His second is called 'Dwiteeya Purushavatar' in which He enters into and becomes omnipresent in the universe. His third is called 'Triteeya Purushavatar', which is the Vishnu of our brahmand. It is in this form that He resides in the heart of every soul. All other avatars like Varaha avatar, Matsya avatar, etc, are the descensions of Vishnu of this brahmand, Who takes various forms necessary for the maintainance and protection of our brahmand. There are some exceptions, like when Krishna or Ram come on our earth, it is not the avatar of Vishnu of this brahmand; it is the direct descension of Bhagwan Shree Krishna of Golok abode.
One should always remember that these different forms and avatars are not less than Shree Krishna or separate from Him. They are all Shree Krishna Himself. He manifests Himself in these forms eternally. They are called His ansh because in these forms He is only manifesting a part of His power outwardly; but inwardly, there is no difference between the various forms - they are all whole, complete, Divine and eternal.