Images are cached by the browser (both disk cache and memory cache) and the cache is used whether the image is loaded via HTML present in the page, .innerHTML
set by a script, .src
set by script or a new image object created by script. In all cases, the browser uses the cache unless the client has caching turned off or the server has instructed the browser not to cache a particular resource. So, if the image is already in the cache, it will be available immediately when set by script. When an image is found in the cache, it is not loaded from the server, it is loaded directly from the local cache.
Images set by script are loaded asynchronously in the background.
If you are worried about the very first time the image is accessed (or any time it ages out of the cache), yes it will have to be loaded from the server. If that short delay while the image is loaded causes a performance issue or delay, then the image can be pre-cached by loading it before it is needed, thus causing it to be cached by the browser so it will be available immediately later on.
You can relieve load on your server by making sure that you have optimal header settings on image retrievals to allow as much image caching by the browser as feasible. You can read what Google has to say about HTTP caching here and here's A Beginner's Guide to HTTP Cache Headers.
FYI, here are some references for how to manually preload images (into the cache):
Image preloader javascript that supports events
Is it possible to insert images in cache before rendering
Is there a way to load images to user's cache asynchronously?