a) first of all, check your firmware version (if you already have the newest one, you do not need this tutorial)
b) download and install latest Flashtool from this Post
c) download also latest Android 5.1.1 (mirror Google Drive) for Sony XPERIA Z
d) paste the ftf file downloaded in step c) to folder „Firmware“ included in the XPERIA Flashtool
e) open the XPERIA Flashtool, click on flash and choose downloaded firmware, than click ok. Important - if you do not want to delete any data of your phone, just uncheck flashing userdata.sin file. All data will remain in your phone, but sometimes it can cause additional troubles. So it is better (and I recommend that) to do the backup of your personal data, than proceed with clean instalation and then restore your personal data.
f) turn off your phone and when Flashtool shows pop up window, connect phone to PC with holding volume down button
g) after few minutes all is done and your phone gets the new firmware